[Programmers]-주식가격(스택/큐)

풀었던 코드

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

다시풀기

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

요약

변수없이 써졌다.