-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMain.java
More file actions
49 lines (39 loc) · 1.28 KB
/
Main.java
File metadata and controls
49 lines (39 loc) · 1.28 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
46
47
48
49
import java.io.*;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
private static int N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
Stack<Tower> stack = new Stack<>();
N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
int height = Integer.parseInt(st.nextToken());
Tower tower = new Tower(i, height);
while(!stack.isEmpty()){
if(stack.peek().height >= height) {
sb.append(stack.peek().idx + 1).append(" ");
break;
} else {
stack.pop();
}
}
if(stack.isEmpty()) {
sb.append("0 ");
}
stack.push(tower);
}
System.out.println(sb.toString().trim());
}
public static class Tower{
private int idx;
private int height;
public Tower(int idx, int height) {
this.idx = idx;
this.height = height;
}
}
}