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

 

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

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

programmers.co.kr

 

스택/큐 문제이다. (LEVEL2)

 

풀이 설명

: prices의 첫 번째 원소부터 배열 끝까지 가면서 가격이 떨어질 때까지 count를 +1 해준다.

 

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

+ Recent posts