practivceAlgorithm/백준

[백준][Python] 1713 후보 추천하기

findTheValue 2021. 10. 2. 18:27

사진틀을 dict로 관리 갯수 초과하면 하나 삭제

 

import sys
input = sys.stdin.readline
from collections import defaultdict

N = int(input())
n = int(input())
nums = list(map(int, input().split()))
count = defaultdict(int)
for i in range(n):
    count[nums[i]] += 1
    if len(count) > N:
        min_val = float('inf')
        for candidate in count:
            if candidate == nums[i]:
                continue
            if count[candidate] < min_val:
                min_val = count[candidate]
                target = candidate
        del count[target]
answer = []
for candidate in count:
    answer.append(candidate)
print(*sorted(answer))