스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다.
제한사항
이중 딕셔너리를 이용하자
def solution(genres, plays):
answer = []
# 장르를 key, {고유번호 : 재생횟수}를 value로 하는 딕셔너리
dic = {}
for i in range(len(plays)):
if genres[i] in dic:
dic[genres[i]][i] = plays[i]
else:
dic[genres[i]] = { i : plays[i] }
# 장르 총 재생횟수를 기준으로 정렬
dic = sorted(dic.items(), key=lambda x: sum(x[1].values()), reverse=True)
# 노래 별 재생횟수를 기준으로 정렬한 후 answer에 두 개 저장
for value in dic:
sub_dic = sorted(value[1].items(), key=lambda item: item[1],reverse=True)
answer.append(sub_dic[0][0])
if len(sub_dic) > 1:
# 장르별 노래가 두 개 이상인 경우
answer.append(sub_dic[1][0])
return answer