๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐Ÿฆ• ๊ณต๋ฃก์ด ๋˜์ž!

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Java ์ฃผ์‹๊ฐ€๊ฒฉ ๋ณธ๋ฌธ

Development/CodingTest

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] Java ์ฃผ์‹๊ฐ€๊ฒฉ

Kirok Kim 2021. 12. 13. 21:42
class Solution {
    public int[] solution(int[] prices) {
        int[] answer = new int[prices.length];
        
      for (int i = 0; i < prices.length; i++) {
				int cnt = 0;
				for (int j = i + 1; j < prices.length; j++) {
					if (i == prices.length - 1) {
						cnt = 0;
						break;
					} else if (prices[i] <= prices[j]) {
						cnt++;
					} else if (prices[i] > prices[j]) {
						cnt++;
						break;
					}
				}
			answer[i] = cnt;
	    }
             return answer;
		}
}
  • ๋ฆฌํ„ด๋˜๋Š” ํšŸ์ˆ˜๋ฅผ cnt๋ผ๊ณ  ์ง€์ •์„ํ•˜๊ณ  1์ดˆ ๋’ค์˜ ๊ฐ€๊ฒฉ๊ณผ ๋น„๊ตํ•˜์—ฌ cnt๋ฅผ ์ธก์ •ํ–ˆ๋‹ค.
  • ์กฐ๊ธˆ ๋” ๊ฐ„๋‹จํžˆ ํ•˜๋ ค๋ฉด cnt๋ฅผ ์—†์• ๊ณ  answer์˜ ๋ฐฐ์—ด์— ++์‹œํ‚ค๋ฉด ์กฐ๊ธˆ ๋” ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.
  • ์•„๋ž˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฌธ์ œ ํ’€์ด๋‹ค.
class Solution {
    public int[] solution(int[] prices) {
        int len = prices.length;
        int[] answer = new int[len];
        int i, j;
        for (i = 0; i < len; i++) {
            for (j = i + 1; j < len; j++) {
                answer[i]++;
                if (prices[i] > prices[j])
                    break;
            }
        }
        return answer;
    }
}

 

๋ฐ˜์‘ํ˜•
Comments