import java.util.*;

class Solution {
    public int[] solution(int[] prices) {
        Stack<Price> stack = new Stack<>();
        int[] answer = new int[prices.length];

        for(int time = 0 ; time < prices.length; time++){
            Price price = new Price(prices[time], time);
            
            if(stack.isEmpty()){
                stack.push(price);
                continue;
            }
            
            Price peekPrice = stack.peek();
            if(peekPrice.value <= price.value){
                stack.push(price);
            }else{
                //픽같은거 변수로하지말자걍
                //while(!stack.isEmpty() && peekPrice.value > price.value){
                while(!stack.isEmpty() && stack.peek().value > price.value){
                    Price popPrice = stack.pop();
                    answer[popPrice.time] = price.time - popPrice.time;
                }
                stack.push(price);
            }
        }
        while(!stack.isEmpty()){
            Price price = stack.pop();
            //System.out.println(price.value+" "+price.time);
            answer[price.time] = prices.length -1 - price.time;
        }
        return answer;
    }
    
    private static class Price{
        int value;
        int time;
        
        Price( int value, int time){
            this.value = value;
            this.time = time;
        }
    }
}
import java.util.Stack;

public class Solution {
    public int[] solution(int[] prices) {
        int[] answer = new int[prices.length];

        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < prices.length; i++) {
            while (!stack.isEmpty() && prices[stack.peek()] > prices[i]) {
                int index = stack.pop();
                answer[index] = i - index;
            }

            stack.push(i);
        }

        while (!stack.isEmpty()) {
            int index = stack.pop();
            answer[index] = prices.length - index - 1;
        }
        return answer;
    }
}