[코딩테스트] 프로그래머스 - 주식가격(Java)

proman·2022년 9월 9일
0

Coding-Test

목록 보기
9/21
post-thumbnail

👘 설명

레벨:2
언어:Java

🏉 느낀점

해당문제가 스택/큐 항목으로 분류되어있는데 나도 그렇고, 다른사람들 답들 확인해보면 스택/큐 활용하지를 않았는데... ㅎ

일단 스택/큐 사용하지않으면 레벨1정도중에서 상으로 쳐도되지않을까싶은 난이도입니다.

🗝 내가 작성한 코드

class Solution {
    public int[] solution(int[] prices) {
        
        int[] answer = new int[prices.length];
        
        for(int i = 0, count = 0; i < prices.length; answer[i] = count, count = 0, i++) {       
            for(int j = i+1; j < prices.length; j++) {
                count++;
                if(prices[i] > prices[j]) break;
            }
        }
        
        return 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;
    }
}

0개의 댓글