SHyeonL / Future_Internet_LAB_Algorithm

미래 인터넷 연구실 알고리즘 스터디
0 stars 0 forks source link

17299 - 오등큰수 #12

Open SHyeonL opened 1 year ago

SHyeonL commented 1 year ago
# 17299번 - 오등큰수
import sys

input = sys.stdin.readline

n = int(input())
s = list(map(int, (input().split())))
freq = [0 for _ in range(max(s) + 1)]
ans = [0 for _ in range(n)]
stack = []
for i in range(n):
    freq[s[i]] += 1
stack.append(0)
for i in range(1, n):
    if not stack:
        stack.append(i)
    while stack and freq[s[stack[-1]]] < freq[s[i]]:
        ans[stack[-1]] = s[i]
        stack.pop()
    stack.append(i)
while stack:
    ans[stack[-1]] = -1
    stack.pop()
print(*ans)

딕셔너리 키를 이용한 카운트 방법 찾아보자

yuneojin commented 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)