백준 [ALGORITHM] - 빈도 정렬 (2910)
2024. 1. 20. 16:39ㆍ코딩/백준 [ALGORITHM]
반응형
N, C = map(int, input().split())
num = list(map(int, input().split()))
print_list = list()
hash_map = dict()
for i in range(N):
if str(num[i]) in hash_map.keys():
hash_map[str(num[i])][0] += 1
else:
hash_map[str(num[i])] = [1, i]
hash_map = sorted(hash_map.items(), key=lambda x: (x[1][0], -x[1][1]), reverse=True)
for i in range(len(hash_map)):
#print(hash_map[i][0])
#print(hash_map[i][1][1])
print_list.extend(([int(hash_map[i][0])] * int(hash_map[i][1][0])))
str1 = ' '.join(str(s) for s in print_list)
print(str1)
해시맵(dict)를 활용하여 코드 작성함
반응형
'코딩 > 백준 [ALGORITHM]' 카테고리의 다른 글
백준 [ALGORITHM] - 숫자 카드 (10815) (0) | 2024.01.24 |
---|---|
백준 [ALGORITHM] - A와 B (12904) (0) | 2024.01.21 |
백준[ALGORITHM] - 팩토리얼 0의 개수 (1676) (0) | 2023.10.31 |
백준 [ALGORITHM] - 카드 놓기 (5568) (0) | 2023.06.12 |
백준 [ALGORITHM] - 명령 프롬프트 (1032) (0) | 2023.06.11 |