Counter map을 만든 후 set에 조사할 count들을 모은 후 순회하며 그 cnt보다 크면 cnt에 맞춰주고 작으면 0에 맞춰주는 식으로 풀었습니다.
import sys
input = sys.stdin.readline
from collections import defaultdict
for test in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
counter = defaultdict(int)
for num in arr:
counter[num] += 1
candidate = set()
for target in counter.values():
candidate.add(target)
answer = float('inf')
for target in candidate:
total = 0
for cnt in counter.values():
if cnt == target:
continue
if target > cnt:
total += cnt
else:
total += cnt - target
if total >= answer:
break
answer = min(answer, total)
print(answer)
'practivceAlgorithm > codeforce' 카테고리의 다른 글
[codeforce][Python] #690 B. Last Year's Substring (0) | 2021.12.07 |
---|---|
[codeforce][Python] #690 A. Favorite Sequence (0) | 2021.12.07 |
[Codeforce][Python] #702 D. Permutation Transformation (0) | 2021.11.07 |
[Codeforce][Python] #702 E. Accidental Victory (0) | 2021.11.07 |
[Codeforce][Python] #702 C. Sum of Cubes (0) | 2021.11.07 |