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;
}