๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์ž๋ฃŒ๊ตฌ์กฐ & ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ฃผ์‹ ๊ฐ€๊ฒฉ (Java)

by 1mj 2022. 2. 6.

 

https://programmers.co.kr/learn/courses/30/lessons/42584

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ฃผ์‹๊ฐ€๊ฒฉ

์ดˆ ๋‹จ์œ„๋กœ ๊ธฐ๋ก๋œ ์ฃผ์‹๊ฐ€๊ฒฉ์ด ๋‹ด๊ธด ๋ฐฐ์—ด prices๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ€๊ฒฉ์ด ๋–จ์–ด์ง€์ง€ ์•Š์€ ๊ธฐ๊ฐ„์€ ๋ช‡ ์ดˆ์ธ์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”. ์ œํ•œ์‚ฌํ•ญ prices์˜ ๊ฐ ๊ฐ€๊ฒฉ์€ 1 ์ด์ƒ 10,00

programmers.co.kr

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ณ ๋“์  Kit ์ฃผ์‹๊ฐ€๊ฒฉ ๋ฌธ์ œ ํ’€์ด์ด๋‹ค.

 

๋ช‡ ์ดˆ๋งŒ์— ์ฃผ์‹์ด ๋–จ์–ด์กŒ๋Š”์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ธ๋ฐ ๋ฐฐ์—ด ์ธ๋ฑ์Šค์˜ ์ฐจ์ด๋กœ ์‰ฝ๊ฒŒ ์ดˆ๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ณ  ํ˜„์žฌ ๋ฐ˜๋ณต ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ทธ ๋’ค์— ๋–จ์–ด์ง„ ์ ์ด ์žˆ๋Š”์ง€ ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ํ™•์ธํ–ˆ๋‹ค.

 

์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ ค ํšจ์œจ์„ฑ์—์„œ ํƒˆ๋ฝํ•  ์ค„ ์•Œ์•˜๋Š”๋ฐ ๋ฐฐ์—ด์„ ์ปฌ๋ ‰์…˜์œผ๋กœ ๋ณ€ํ™˜ํ•˜์ง€ ์•Š๊ณ  ์ˆœ์ˆ˜๋ฐฐ์—ด๋กœ ์‚ฌ์šฉํ•ด์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™๋‹ค.

 

class Solution {
    public int[] solution(int[] prices) {
        int len = prices.length;
        int[] answer = new int[len];
        
        for (int i = 0; i < answer.length; i++) {
            for (int j = i + 1; j < answer.length; j++) {
                if (prices[i] > prices[j]) {
                    answer[i] = j-i;
                    break;
                }
                if (j == len - 1) {
                    answer[i] = j - i;
                }
            }
        }
        return answer;
    }
}

 

 

๋Œ“๊ธ€