-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_2493.java
More file actions
45 lines (38 loc) · 1.5 KB
/
BOJ_2493.java
File metadata and controls
45 lines (38 loc) · 1.5 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.io.*;
import java.util.*;
public class BOJ_2493 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
int[] tops = new int[n];
for(int i = 0; i < n; i++) {
tops[i] = Integer.parseInt(st.nextToken());
}
Stack<int[]> stack = new Stack<>();
for(int i = 0; i < n; i++) {
if (stack.isEmpty()) { // 스택이 비어있다면
sb.append("0 ");
stack.push(new int[] {i, tops[i]});
} else {
while (true) {
if (stack.isEmpty()) { // 스택이 비어있다면
sb.append("0 ");
stack.push(new int[] {i, tops[i]});
break;
}
if (stack.peek()[1] > tops[i]) { // peek한 탑의 높이가 현재 탑의 높이보다 높다면
sb.append(stack.peek()[0]+1 + " ");
stack.push(new int[] {i, tops[i]});
break;
} else { // peek한 탑의 높이가 현재 탑의 높이보다 낮다면
stack.pop();
}
}
}
}
System.out.println(sb);
}
}