From 4e64fb008cfe3c38a519aebb2676fdc699018553 Mon Sep 17 00:00:00 2001 From: SSUM <116950962+ssum21@users.noreply.github.com> Date: Sat, 15 Feb 2025 14:48:30 +0900 Subject: [PATCH] =?UTF-8?q?[level=203]=20Title:=20=EB=B2=A0=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=95=A8=EB=B2=94,=20Time:=200.10=20ms,=20Memory:=201?= =?UTF-8?q?0.2=20MB=20-BaekjoonHub?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 프로그래머스/3/42579. 베스트앨범/README.md | 4 +- .../3/42579. 베스트앨범/베스트앨범.py | 37 ++++++++++--------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/프로그래머스/3/42579. 베스트앨범/README.md b/프로그래머스/3/42579. 베스트앨범/README.md index a595adb..5b97252 100644 --- a/프로그래머스/3/42579. 베스트앨범/README.md +++ b/프로그래머스/3/42579. 베스트앨범/README.md @@ -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 ### 문제 설명 diff --git a/프로그래머스/3/42579. 베스트앨범/베스트앨범.py b/프로그래머스/3/42579. 베스트앨범/베스트앨범.py index 4e5595b..c664a0e 100644 --- a/프로그래머스/3/42579. 베스트앨범/베스트앨범.py +++ b/프로그래머스/3/42579. 베스트앨범/베스트앨범.py @@ -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 \ No newline at end of file + # (재생 횟수 내림차순, 고유 번호 오름차순) 정렬 후 최대 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 +