728x90
😉 풀이
def solution(genres, plays):
answer = []
# 플레이 총합 기록
dt = {}
# 각 장르별 고유번호-플레이 횟수 기록
idxs = {}
leng = len(genres)
for i in range(leng):
genre = genres[i]
play = plays[i]
val = dt.get(genre, 0)
# 플레이 횟수 합산
dt[genre] = val + play
valval = idxs.get(genre, [])
# 플레이 기록 저장
valval.append({
'idx': i,
'play': play
})
idxs[genre] = valval
# 총 플레이 횟수 내림차순 정렬
dt = sorted(dt.items(), key = lambda item : -item[1])
for data in dt:
ranks = idxs[data[0]]
# 장르 내 플레이 횟수 내림차순 정렬, 동일 시 고유번호 오름차순 정렬
ranks.sort(key = lambda x : (-x['play'], x['idx']))
cnt = 0
ranks_len = len(ranks)
if ranks_len == 1:
answer.append(ranks[0]['idx'])
else:
for i in range(2):
answer.append(ranks[i]['idx'])
return answer
✔️ 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42579?language=python3#