Skip to content

Commit 1dadb52

Browse files
authored
Merge pull request #596 from AlgorithmWithGod/JHLEE325
[20250802] BOJ / G4 / 오큰수 / 이준희
2 parents 34706a1 + cfa370d commit 1dadb52

File tree

1 file changed

+41
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)