알고리즘 문제 풀이/Python

python 백준 1302 베스트셀러

맛대 2022. 6. 16. 20:24

https://www.acmicpc.net/problem/1302

python의 dictionary 활용

  • dictionary에 해당 책이 없을경우 key=책 이름, value=1 추가
  • dictionary에 해당 책이 있을경우 value 값에 +1
N = int(input())
total = {}
for i in range(N):
    temp = input()
    if total.get(temp):
        total[temp] += 1
    else:
        total[temp] = 1
ans = ''
cnt = 0
for item in total.items():
    if item[1] > cnt:
        ans = item[0]
        cnt = item[1]
    elif item[1] == cnt and ans > item[0]:
        ans = item[0]
print(ans)