[백준] 2910번 빈도 정렬

거북이·2023년 1월 29일
0

백준[실버2]

목록 보기
43/81
post-thumbnail

💡문제접근

  • 딕셔너리를 이용해서 해당 key값을 문자로 처리한 다음 value값만큼 곱해준다.

💡코드(메모리 : 30616KB, 시간 : 36ms)

import sys
input = sys.stdin.readline

dict = {}
N, C = map(int, input().strip().split())
message = list(map(int, input().strip().split()))

for i in message:
    if i not in dict:
        dict[i] = 1
    else:
        dict[i] += 1

new_dict = sorted(dict.items(), key = lambda x : (-x[1]))
for i in range(len(new_dict)):
    print((str(new_dict[i][0]) + " ") * new_dict[i][1], end = "")

💡소요시간 : 4m

0개의 댓글