Skip to content

Commit 73d34a8

Browse files
authored
[20250726] BOJ / G4 / 오큰수 / 이강현
1 parent d912fc1 commit 73d34a8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int[] arr;
10+
static int[] NGE;
11+
12+
public static void main(String[] args) throws Exception {
13+
int N = Integer.parseInt(br.readLine());
14+
arr = new int[N];
15+
st = new StringTokenizer(br.readLine());
16+
for (int i = 0; i < N; i++) {
17+
arr[i] = Integer.parseInt(st.nextToken());
18+
}
19+
if(N == 1){
20+
bw.write("-1");
21+
bw.close();
22+
return;
23+
}
24+
25+
NGE = new int[N];
26+
ArrayDeque<Integer> stk = new ArrayDeque<>();
27+
stk.push(0);
28+
for (int i = 1; i < N; i++) {
29+
while(!stk.isEmpty() && arr[stk.peek()]<arr[i]){
30+
NGE[stk.poll()] = arr[i];
31+
}
32+
stk.push(i);
33+
}
34+
while(!stk.isEmpty()){
35+
NGE[stk.poll()] = -1;
36+
}
37+
for (int i = 0; i < N; i++) {
38+
bw.write(NGE[i]+" ");
39+
}
40+
bw.close();
41+
}
42+
}
43+
```

0 commit comments

Comments
 (0)