Open SHyeonL opened 1 year ago
import sys
from collections import Counter
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
F = Counter(A)
stack = [0]
NGF = [-1]*N
for i in range(1, len(A)):
while stack and F[A[stack[-1]]] < F[A[i]]:
NGF[stack.pop()] = A[i]
stack.append(i)
print(*NGF)
딕셔너리 키를 이용한 카운트 방법 찾아보자