Skip to content

Commit f0d1e3f

Browse files
authored
Merge pull request #1425 from AlgorithmWithGod/JHLEE325
[20251116] BOJ / G3 / 오등큰수 / 이준희
2 parents 853aaf8 + 0b9eaba commit f0d1e3f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int N = Integer.parseInt(st.nextToken());
10+
int[] arr = new int[N];
11+
st = new StringTokenizer(br.readLine());
12+
for (int i = 0; i < N; i++) {
13+
arr[i] = Integer.parseInt(st.nextToken());
14+
}
15+
16+
int mnum = 1000000;
17+
int[] count = new int[mnum + 1];
18+
for (int i = 0; i < N; i++) {
19+
count[arr[i]]++;
20+
}
21+
22+
int[] result = new int[N];
23+
Stack<Integer> stack = new Stack<>();
24+
25+
for (int i = N - 1; i >= 0; i--) {
26+
int f = count[arr[i]];
27+
while (!stack.isEmpty() && count[stack.peek()] <= f) {
28+
stack.pop();
29+
}
30+
if (stack.isEmpty()) {
31+
result[i] = -1;
32+
} else {
33+
result[i] = stack.peek();
34+
}
35+
stack.push(arr[i]);
36+
}
37+
38+
StringBuilder sb = new StringBuilder();
39+
for (int i = 0; i < N; i++) {
40+
sb.append(result[i]).append(' ');
41+
}
42+
System.out.println(sb.toString());
43+
}
44+
}
45+
```

0 commit comments

Comments
 (0)