[백준] 1713번 후보 추천하기

거북이·2023년 10월 2일
0

백준[실버1]

목록 보기
63/67
post-thumbnail

💡문제접근

  • 딕셔너리를 이용해서 해결할 수 있었다.
  • Key는 후보자의 번호, Value는 리스트 형식으로 [추천 수, 들어온 순서]로 넣고 사진틀에 게시할 수 있는 경우와 게시할 수 없는 경우에 대한 처리를 잘해주면 된다.

💡코드(메모리 : 31256KB, 시간 : 44ms)

import sys
input = sys.stdin.readline

picture_frame = {}
N = int(input())
cnt = int(input())
sequence = list(map(int, input().strip().split()))

# 추천수, 들어온 순서
for i in range(cnt):
    # 사진틀에 없는 경우
    if sequence[i] not in picture_frame:
        if len(picture_frame) < N:
            picture_frame[sequence[i]] = [1, i]
        else:
            delete = sorted(picture_frame.items(), key=lambda x : (x[1][0], x[1][1]))
            del_num = delete[0][0]
            del picture_frame[del_num]
            picture_frame[sequence[i]] = [1, i]
    # 사진틀에 있는 경우
    else:
        picture_frame[sequence[i]][0] += 1

key_list = sorted(picture_frame.keys())
print(*key_list)

📌딕셔너리의 Key:Value 쌍을 삭제하는 경우 : del 명령어

💡소요시간 : 27m

0개의 댓글