개발/코딩테스트

[프로그래머스]베스트앨범 - 파이썬(Python)

grulsuitg 2022. 7. 2. 16:54

문제 링크 :https://programmers.co.kr/learn/courses/30/lessons/42579

 

코딩테스트 연습 - 베스트앨범

스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다. 속한 노래가

programmers.co.kr


My Solution

from collections import defaultdict
from collections import Counter


def solution(genres, plays):
    answer = []
    n = len(genres)
    times = defaultdict(dict)
    total = defaultdict(int)
    for idx, (genre, play) in enumerate(zip(genres, plays)):
        times[genre][idx] = play
        total[genre] += play

    for genre in Counter(total).most_common():
        for idx, time in Counter(times[genre[0]]).most_common(2):
            answer.append(idx)

    return answer

 

Key Idea

  • Counter 를 이용해 최빈값 구하기
  • total 로 장르 별 전체 값, times 로 장르별 최빈 idx를 관리.

Best solution 은 가독성, 간결함에서 이점이 없는 것 같아 첨부 X