[백준] 1302. 베스트셀러

주형(Jureamer)·2022년 4월 15일
0

입력값이 많을 경우 단순 input()을 사용할 때, 백준에서는 속도가 느려 시간 초과가 날 수 있다. 이유는 sys.stdin.readline()과 비교해서 prompt message를 출력하고, 개행 문자를 삭제한 값을 리턴하기 때문에 느리다. [Python] Input vs. sys.stdin.readline 차이점?을 참조하자!

그래서 input = sys.stdin.readline으로 명시 해주는 편이 좋다. 다만 개행 문자를 포함하기에 개행 문자 삭제가 필요한 경우 strip, rstrip, lstrip 등의 메소드를 이용하여 별도 처리 해줘야 됨

해당 문제는 dictionary 맵(JS에서 객체)을 이용하여 풀었다.

# -*- coding: utf8 -*-
# 시간제한 2초(초당 1~2억 연산), 메모리 제한 128MB
# 1 <= N <= 1000

import sys
input = sys.stdin.readline

books = {}
for i in range(int(input())):
  book = input()
  
  if book not in books:
    books[book] = 1
  else: 
    books[book] += 1

max_sell = max(books.values())
bestseller = []

for book, number in books.items():
  if number == max_sell:
    bestseller.append(book)

print(sorted(bestseller)[0])
profile
작게라도 꾸준히 성장하는게 목표입니다.

0개의 댓글