(Python) 프로그래머스 - 베스트앨범

[문제 링크]

풀이

def solution(genres, plays):
    answer = []
    store = dict()
    length = len(genres)

    for idx in range(length):
        if store.get(genres[idx]) is None:
            store[genres[idx]] = [plays[idx], [(plays[idx], idx)]]
        else:
            store[genres[idx]][0] += plays[idx]
            store[genres[idx]][1].append((plays[idx], idx))

    temp = sorted(store.values(), reverse=True)

    for _, data in temp:
        data.sort(key=lambda x: (-x[0], x[1]))
        answer.append(data[0][1])
        if len(data) >= 2:
            answer.append(data[1][1])

    return answer

© 2021. By Backtony