230221 주식가격

Jongleee·2023년 2월 21일
0

TIL

목록 보기
187/576
public static int[] solution(int[] prices) {
    int n = prices.length;
    Stack<Integer> stack = new Stack<>();
    int[] answer = new int[n];
    for (int i = 0; i < n; i++) {
        while (!stack.isEmpty() && prices[i] < prices[stack.peek()]) {
            int j = stack.pop();
            answer[j] = i - j;
        }
        stack.push(i);
    }
    while (!stack.isEmpty()) {
        int j = stack.pop();
        answer[j] = n - j - 1;
    }

    return answer;
}

0개의 댓글