[Link] https://programmers.co.kr/learn/courses/30/lessons/42584?language=java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;
class Solution {
public int[] solution(int[] prices) {
int[] answer = new int[prices.length];
int polledIdx = 0, curIdx;
Stack<Integer> stack = new Stack<>();
for(curIdx = 0; curIdx < prices.length; curIdx++) {
int price = prices[curIdx];
while(!stack.isEmpty() && prices[stack.peek()] > price) {
int idx = stack.pop();
answer[idx] = curIdx - idx;
}
stack.add(curIdx);
}
curIdx--;
while(!stack.isEmpty()) {
int idx = stack.pop();
answer[idx] = curIdx - idx;
}
return answer;
}
}