
- 난이도: Lv2
 
프로그래머스 링크: https://school.programmers.co.kr/learn/courses/30/lessons/42584
풀이 링크(GitHub): hayannn/CodingTest_Java/프로그래머스/2/주식가격


풀이 시간 : 35분
import java.util.*;
class Solution {
    public int[] solution(int[] prices) {
        int n = prices.length;
        int[] answer = new int[n];
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < n; i++) {
            while (!stack.isEmpty() && prices[i] < prices[stack.peek()]) {
                int idx = stack.pop();
                answer[idx] = i - idx;
            }
            stack.push(i);
        }
        while (!stack.isEmpty()) {
            int idx = stack.pop();
            answer[idx] = n - idx - 1;
        }
        
        return answer;
    }
}


