본문 바로가기
알고리즘/프로그래머스 level 3

[파이썬🐍] 프로그래머스 : 베스트앨범

by 코딩개미뚠뚠 2021. 6. 24.
반응형
def solution(genres, plays):
    answer = []
    playDic = {}  
    dic = {}      
    
    for i in range(len(genres)):
        playDic[genres[i]] = playDic.get(genres[i], 0) + plays[i] 
        dic[genres[i]] = dic.get(genres[i], []) + [(plays[i], i)]
    
    genreSort = sorted(playDic.items(), key = lambda x: x[1], reverse = True)
    
    for (genre, totalPlay) in genreSort:
        dic[genre] = sorted(dic[genre], key = lambda x: (-x[0], x[1])) #내림차순, 오름차순
        answer += [idx for (play, idx) in dic[genre][:2]]
    
    return answer
반응형

댓글