CodingTest/Baekjoon

[baekjoon] 백준 1302번(파이썬): 베스트셀러

JunJangE 2021. 10. 12. 12:35

문제

 

1302번: 베스트셀러

첫째 줄에 오늘 하루 동안 팔린 책의 개수 N이 주어진다. 이 값은 1,000보다 작거나 같은 자연수이다. 둘째부터 N개의 줄에 책의 제목이 입력으로 들어온다. 책의 제목의 길이는 50보다 작거나 같고

www.acmicpc.net

알고리즘

- collections 모듈에 Counter 클래스를 사용한다.

- 람다 정렬을 통해 개수를 내림차순으로, 이름을 오름차순으로 정렬한다.

코드

import sys
from collections import Counter

n = int(sys.stdin.readline())
temp = []
for _ in range(n):
    temp.append(str(sys.stdin.readline().strip()))

res = Counter(temp)
# 람다 정렬을 통해 개수를 내림차순으로 이름을 오름차순으로 정렬
res = sorted(res.items(), key=(lambda x: (-x[1], x[0])))
# 출력
print(res[0][0])

github

 

GitHub - junjange/CodingTest: 내가 푼 코딩 테스트 문제와 해결법

내가 푼 코딩 테스트 문제와 해결법. Contribute to junjange/CodingTest development by creating an account on GitHub.

github.com