Skip to content

Commit 8aaab1c

Browse files
authored
Merge pull request #1399 from AlgorithmWithGod/LiiNi-coder
[20251113] PGM / LV2 / 주식가격 / 이인희
2 parents bda099e + a2e0ac9 commit 8aaab1c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
```java
2+
import java.util.*;
3+
4+
class Solution {
5+
public int[] solution(int[] prices) {
6+
int n = prices.length;
7+
int[] answer = new int[n];
8+
ArrayDeque<Integer> stack = new ArrayDeque<>();
9+
10+
for(int i= 0; i < n; i++){
11+
while(!stack.isEmpty()){
12+
if(prices[stack.peekLast()] > prices[i]){
13+
int index = stack.pollLast();
14+
answer[index] = i - index;
15+
}else{
16+
break;
17+
}
18+
}
19+
stack.offerLast(i);
20+
}
21+
22+
23+
while(!stack.isEmpty()){
24+
int idx = stack.pollLast();
25+
answer[idx] = n - 1 - idx;
26+
}
27+
return answer;
28+
}
29+
}
30+
31+
```

0 commit comments

Comments
 (0)