
문제 접근
17298 오큰수 문제와 비슷하게 접근
cnt 배열을 만들어 비교
풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class G17299 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
int[] A = new int[N];
int[] cnt = new int[1_000_001];
String[] split = br.readLine().split(" ");
for (int i = 0; i < N; i++) {
A[i] = Integer.parseInt(split[i]);
cnt[A[i]] += 1;
}
Stack<Integer> stack = new Stack<>();
for (int j = 0; j < N; j++) {
while (!stack.isEmpty() && cnt[A[stack.peek()]] < cnt[A[j]]) {
A[stack.pop()] = A[j];
}
stack.push(j);
}
while (!stack.isEmpty()) {
A[stack.pop()] = -1;
}
for (int k = 0; k < N; k++) {
sb.append(A[k]).append(" ");
}
System.out.println(sb);
}
}
'Algorithm' 카테고리의 다른 글
수학 / 백준 2609 최대공약수와 최소공배수 (0) | 2025.04.14 |
---|---|
수학 / 백준 10430 나머지 (0) | 2025.04.13 |
자료구조 / 백준 17298 오큰수 (0) | 2025.04.09 |
자료구조 / 백준 10799 쇠막대기 (0) | 2025.04.08 |
자료구조 / 백준 17413 단어 뒤집기 2 (0) | 2025.04.07 |