본문 바로가기

알고리즘

[프로그래머스] 주식가격 (JAVA)

반응형

코딩테스트 연습 - 주식가격 | 프로그래머스 (programmers.co.kr)

 

코딩테스트 연습 - 주식가격

초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,00

programmers.co.kr

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