[level 3] Title: 베스트앨범, Time: 0.10 ms, Memory: 10.2 MB -BaekjoonHub

This commit is contained in:
SSUM
2025-02-15 14:48:30 +09:00
parent f07b8ec71e
commit 4e64fb008c
2 changed files with 21 additions and 20 deletions

View File

@@ -4,7 +4,7 @@
### 성능 요약
메모리: 10.1 MB, 시간: 0.07 ms
메모리: 10.2 MB, 시간: 0.10 ms
### 구분
@@ -16,7 +16,7 @@
### 제출 일자
2025년 02월 15일 12:17:34
2025년 02월 15일 14:48:28
### 문제 설명

View File

@@ -1,23 +1,24 @@
from collections import defaultdict
import operator
def solution(genres, plays):
music = defaultdict(list)
total_play_music = defaultdict(int)
for i in range(len(genres)):
genre = genres[i]
play_count = plays[i]
total_play_music[genre] += play_count
music[genre].append((play_count,i))
sorted_genres = sorted(total_play_music.keys(), key=lambda x : total_play_music[x], reverse=True)
genre_play_count = defaultdict(int) # 장르별 총 재생 횟수 저장
genre_songs = defaultdict(list) # 장르별 노래 목록 저장
# 데이터 저장
for i, (genre, play) in enumerate(zip(genres, plays)):
genre_play_count[genre] += play
genre_songs[genre].append((play, i))
# 1. 장르별 총 재생 횟수를 기준으로 정렬
sorted_genres = sorted(genre_play_count, key=genre_play_count.get, reverse=True)
answer = []
# 2. 각 장르별 노래를 정렬하여 수록
for genre in sorted_genres:
sorted_songs = sorted(music[genre], key=lambda x:(-x[0],x[1]))
answer.extend([song[1] for song in sorted_songs[:2]])
return answer
# (재생 횟수 내림차순, 고유 번호 오름차순) 정렬 후 최대 2곡 선택
sorted_songs = sorted(genre_songs[genre], key=lambda x: (-x[0], x[1]))[:2]
answer.extend(idx for _, idx in sorted_songs)
return answer