-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj17298.java
More file actions
35 lines (27 loc) · 830 Bytes
/
boj17298.java
File metadata and controls
35 lines (27 loc) · 830 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
32
33
34
35
import java.util.Scanner;
import java.util.Stack;
public class boj17298 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack<Integer> stack = new Stack<Integer>();
int N = sc.nextInt();
int[] num = new int[N];
for (int i = 0; i < N; i++) {
num[i] = sc.nextInt();
}
for (int i = 0; i < N; i++) {
while (!stack.isEmpty() && num[stack.peek()] < num[i]) {
num[stack.pop()] = num[i];
}
stack.push(i);
}
while (!stack.isEmpty()) {
num[stack.pop()] = -1;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
sb.append(num[i]).append(' ');
}
System.out.println(sb);
}
}