-
[SW Expert] 1204번_최빈수구하기Study/알고리즘 2020. 4. 25. 17:31
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
from collections import defaultdict for _ in range(int(input())): d = defaultdict(int) t = int(input()) l = list(map(int,input().split())) for l_ in l: d[l_] += 1 for key,value in d.items(): if value == max(d.values()): print("#{0} {1}".format(t,key)) break
내가 좋아하는 defaultdict...
Pass는 했는데 다시 읽어보니 (단, 최빈수가 여러 개 일 때에는 가장 큰 점수를 출력하라) 라는 조항이 있었다.
알기론 딕셔너리는 (파이썬 3.6부터) 입력순으로 정렬이 되는데 다행히 테스트 케이스 중에서는 최빈수가 여러 개 있을 때 큰 점수가 다 앞쪽에 몰려있었나보다ㅋㅋㅋㅋ어쨌든! 알게 되었으니 다시 풀었다.
for _ in range(int(input())): t = int(input()) l = list(map(int,input().split())) max_cnt = 0 ; ans = 0 for i in range(100,-1,-1): c = l.count(i) if max_cnt < c: max_cnt = c ans = i print("#{0} {1}".format(t,ans))
딕셔너리를 버리고... 점수가 0점 이상 100점 이하인 것과 python 내장함수 count를 이용하여 최빈값을 가진, 그 중에서도 가장 큰 점수를 출력해줬다.
'Study > 알고리즘' 카테고리의 다른 글
[SW Expert] 2005번_파스칼의삼각형 (0) 2020.07.08 [SW Expert] 1926번_간단한369게임 (0) 2020.07.08 [SW Expert] 1859번_백만장자프로젝트 (0) 2020.04.25 [백준] 16194번_카드구매하기2 (0) 2020.03.18 11052번_카드구매하기 (0) 2020.03.18