[Python] 베스트 앨범

966 단어 algorithmalgorithm


1 Try :

from collections import defaultdict
def solution(genres, plays):
    answer = []
    
    gen_dic = defaultdict(list)
    totalP = defaultdict(lambda : 0) 
    
    for i, (g,p) in enumerate(zip(genres,plays)) :
        gen_dic[g].append((i,p))
        totalP[g] += p
    sTP = sorted(totalP.items(), key = lambda x: x[1],reverse = True)
    
    for t in sTP:
        sGD = sorted(gen_dic[t[0]], key=lambda x: x[1], reverse=True)[:2]
        answer += [i[0] for i in sGD]
    
    return answer
  • defaultdict
  • lambda
  • dictionary search

others code :

  • sum, map
  • handling : min(len(temp),2)

좋은 웹페이지 즐겨찾기