Skip to content

Commit 1bbdda6

Browse files
authored
Merge pull request #1726 from AlgorithmWithGod/LiiNi-coder
[20251222] BOJ / G4 / 오큰수 / 이인희
2 parents 706b100 + d4c37e1 commit 1bbdda6

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(
8+
new InputStreamReader(System.in)
9+
);
10+
int N = Integer.parseInt(br.readLine());
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
int[] arr = new int[N];
13+
for(int i = 0; i < N; i++){
14+
arr[i] = Integer.parseInt(st.nextToken());
15+
}
16+
int[] answer = new int[N];
17+
Arrays.fill(answer, -1);
18+
Deque<Integer> stack = new ArrayDeque<>();
19+
20+
for(int i = 0; i < N; i++){
21+
while(!stack.isEmpty() && arr[stack.peekLast()] < arr[i]){
22+
int index = stack.pollLast();
23+
answer[index] = arr[i];
24+
}
25+
stack.offerLast(i);
26+
}
27+
StringBuilder sb = new StringBuilder();
28+
for(int v : answer){
29+
sb.append(v).append(" ");
30+
}
31+
32+
System.out.println(sb.toString());
33+
}
34+
}
35+
36+
```

0 commit comments

Comments
 (0)