프로그래머스: 코딩테스트 연습 - 해시 - 베스트앨범

문제

https://programmers.co.kr/learn/courses/30/lessons/42579

정답

from collections import defaultdict


def solution(genres, plays):
    summary = defaultdict(list)
    plays_summary = defaultdict(int)
    index = 0
    for genre, play in zip(genres, plays):
        summary[genre].append([index, play])
        plays_summary[genre] += play
        index += 1

    by_plays = sorted(plays_summary.items(), key=lambda x: -x[1])
    result = []
    for genre, _ in by_plays:
        for song, _ in sorted(summary[genre], key=lambda x: -x[1])[:2]:
            result.append(song)
    return result