-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_42584.java
More file actions
31 lines (25 loc) ยท 886 Bytes
/
DH_42584.java
File metadata and controls
31 lines (25 loc) ยท 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;
/*
์ฃผ์๊ฐ๊ฒฉ
*/
public class DH_42584 {
public int[] solution(int[] prices) {
int[] answer = new int[prices.length];
Stack<Integer> stack = new Stack();
for (int i = 0; i < prices.length; i++) {
// ์คํ์๋ ์ ์ ์ฆ๊ฐํ๋ ์์๋ก๋ง ์ ์ฅ๋์ด์ผ ํจ
// price[i] < price[stack.peek()] ๋ผ๋ฉด
// price[i] >= prices[stack.peek()]์ด ๋ ๋๊น์ง stack.pop()์ฐ์ฐ์ ํ๋ฉด์ ๊ธฐ๊ฐ ๊ตฌํด์ฃผ๊ธฐ
while (!stack.isEmpty() && (prices[i] < prices[stack.peek()])) {
int idx = stack.pop();
answer[idx] = i - idx;
}
stack.push(i);
}
while (!stack.isEmpty()) {
int current = stack.pop();
answer[current] = prices.length - 1 - current;
}
return answer;
}
}