220v
젝무의 개발새발
220v
전체 방문자
오늘
어제
  • 분류 전체보기 (255)
    • AI (35)
      • ML, DL 학습 (30)
      • 논문 리뷰 (4)
      • 실습 및 프로젝트 (1)
    • Algorithm (145)
      • LeetCode (13)
      • 프로그래머스 (35)
      • 백준 (96)
      • 알고리즘, 문법 정리 (1)
    • Mobile, Application (17)
      • Flutter (10)
      • iOS, MacOS (7)
    • BackEnd (7)
      • Flask (1)
      • Node.js (5)
      • Spring, JSP..etc (1)
    • Web - FrontEnd (18)
      • JavaScript, JQuery, HTML, C.. (12)
      • React (6)
    • DataBase (1)
      • MySQL (1)
      • Firebase Firestore (0)
      • Supabase (0)
    • Git (1)
    • 기타 툴 및 오류 해결 (3)
    • 강의 (5)
      • Database (3)
      • 암호학 (2)
      • 알고리즘 (0)
    • 후기와 회고 (2)
    • 블로그 꾸미기 (1)
    • 일상과 이것저것 (20)
      • 맛집 (12)
      • 세상사는일 (4)
      • 도서리뷰 (1)
      • 이런저런 생각들 (잡글) (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • brute-Force
  • dfs
  • Lis
  • bitmasking
  • 다익스트라
  • topological sort
  • 백준
  • top-down
  • Dynamic Programming
  • union-find
  • Priority Queue
  • 티스토리챌린지
  • simulation
  • 위상 정렬
  • Mathematics
  • 프로그래머스
  • 구현
  • Prefix Sum
  • REACT
  • BFS
  • disjoint set
  • Backtracking
  • IMPLEMENT
  • dp
  • binary search
  • two pointer
  • Greedy
  • 오블완
  • Minimum Spanning Tree
  • implementation

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
220v

젝무의 개발새발

Algorithm/프로그래머스

[프로그래머스] 42579. 베스트앨범

2022. 7. 12. 21:00

Programmers - 42579. 베스트앨범

접근

우선, set으로 장르의 종류를 파악(중복 제거)

dictionary를 이용해 장르별 플레이 횟수, 장르별 index와 index에 따른 play 횟수를 sub-dictionary로 저장.

{'classic': 1450, 'pop': 3100}
{'classic': {0: 500, 2: 150, 3: 800}, 'pop': {1: 600, 4: 2500}}

같은 느낌으로.

 

그리고 총 play수가 가장 많은 장르부터,

곡의 play수가 가장 많은 2개를 뽑아 차례대로 저장시킴.

** 단, play수가 같은 곡은 index가 빠른 순서대로 저장. << 유의

 

def solution(genres, plays):
    numOfgenre = set(genres)
    playBygenre = {}
    indexOfgenre = {}
    for i in range(len(genres)):
        gen = genres[i]
        play = plays[i]
        if gen in playBygenre:
            playBygenre[gen] += play
            indexOfgenre[gen].append([i, play])
        else:
            playBygenre[gen] = play
            indexOfgenre[gen] = [[i, play]]

    answer = []

    for _ in range(len(numOfgenre)):
        target = max(playBygenre, key=playBygenre.get)
        subindex = indexOfgenre[target]

        if len(indexOfgenre[target]) == 1:
            answer.append(subindex[0][0])
        else:
            subindex.sort(key=lambda x: (-x[1], x[0]))
            answer.append(subindex[0][0])
            answer.append(subindex[1][0])

        del playBygenre[target]

    return answer
    'Algorithm/프로그래머스' 카테고리의 다른 글
    • [프로그래머스] 42586. 기능개발
    • [프로그래머스] 42584. 주식가격
    • [프로그래머스] 42583. 다리를 지나는 트럭
    • [프로그래머스] 42577. 전화번호 목록
    220v
    220v
    DGU CSE 20 / Apple Developer Academy @ POSTECH 2nd Jr.Learner.

    티스토리툴바