-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJY_42584.java
More file actions
29 lines (29 loc) ยท 909 Bytes
/
JY_42584.java
File metadata and controls
29 lines (29 loc) ยท 909 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
import java.util.*;
class JY_42584 {
static class State {
int price, time;
public State(int price, int time){
this.price = price;
this.time = time;
}
}
public int[] solution(int[] prices) {
int N = prices.length;
int[] answer = new int[N];
Stack<State> stack = new Stack<>();
for(int i=0; i<N; i++){
// top๋ณด๋ค ์์ ๊ฐ๊ฒฉ์ด ๋์ฌ๋๊น์ง ๋ฐ๋ณต
while(!stack.isEmpty() && stack.peek().price > prices[i]){
State now = stack.pop();
answer[now.time] = i-now.time;
}
stack.add(new State(prices[i], i));
}
// ๋ง์ง๋ง๊น์ง ๋จ์ด์ง์ง ์์ ๊ฐ๊ฒฉ๋ค ์ฒ๋ฆฌ
while(!stack.isEmpty()){
State now = stack.pop();
answer[now.time] = (N-1)-now.time;
}
return answer;
}
}