-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJW_42584.java
More file actions
25 lines (25 loc) ยท 880 Bytes
/
JW_42584.java
File metadata and controls
25 lines (25 loc) ยท 880 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
import java.util.Stack;
class JW_42584 {
public int[] solution(int[] prices) {
int n = prices.length;
int[] answer = new int[n];
// ์ด๊ธฐ๊ฐ ์ค์
for (int i = 0; i < n; i++) {
answer[i] = n - 1 - i;
}
// ์์ ๊ฐ๋ง ์ ์งํ ์คํ
Stack<Integer> stk = new Stack<>();
for (int i = n - 1; i >= 0; i--) {
// ์คํ ๋ด์ ํ์ฌ ๊ฐ๋ณด๋ค ํฐ ๊ฐ์ด ์์ผ๋ฉด pop
while (!stk.isEmpty() && prices[stk.peek()] >= prices[i])
stk.pop();
// ํ์ฌ ์ธ๋ฑ์ค๋ณด๋ค ์์ ๊ฐ์ด ์๋ค๋ฉด
if (!stk.isEmpty())
// ๊ทธ ์ธ๋ฑ์ค๊น์ง๋ ์ฆ๊ฐํ ๊ฒ์ด๋ฏ๋ก ๊ฐ ๊ณ์ฐ
answer[i] -= n - 1 - stk.peek();
// ํ์ฌ ์ธ๋ฑ์ค ์ฝ์
stk.push(i);
}
return answer;
}
}