Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 프로그래머스 #NULL 처리하기
- 프로그래머스 #python #2021카카오 #카카오코테 #카카오인턴쉽
- 백준 #백준2217 #백준로프 #python
- 동
- 프로그래머스 #네트워크 #c++ #코딩테스트 #코테 #코테준비 #dfs
- 카카오 코테
- 그리디알고리즘 #그리디 #백준 #우선순위큐 #최소힙 #최대힙 #알고리즘 #코딩테스트 #python
- 프로그래머스 #sql #mysql #코딩테스트
- 백준 #이거다시풀기
- 카카오 #프로그래머스 #python #코딩테스트 #오픈채팅방
- 백준 #백준알고리즘 #알고리즘 #코딩테스트 #코딩테스트준비 #코테준비 #백준2110 #python #문제풀이
- 프로그래머스 #c++ #코딩테스트
- 프로그래머스 #python #코딩테스트 #코테공부 #알고리즘 #dict
Archives
- Today
- Total
say repository
[프로그래머스] 베스트앨범 python (*) 본문
728x90
https://programmers.co.kr/learn/courses/30/lessons/42579
출처 : 프로그래머스
코딩테스트 연습 - 베스트앨범
스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다. 속한 노래가
programmers.co.kr
def solution(genres, plays):
answer = []
arr = sorted([[genres[i], plays[i], i] for i in range(len(genres))],key = lambda x:(x[0],-x[1], x[2]))
# 재생횟수
total_play = {}
for i in arr:
if i[0] not in total_play:
total_play[i[0]] = i[1]
else:
total_play[i[0]] += i[1]
total_play = sorted(total_play.items(), key = lambda x:-x[1]) # 재생횟수가 더 많은 타입부터
for t in total_play:
cnt = 0
for i in arr:
if t[0] == i[0]: # 재생횟수 많은 노래 타입과 arr의 타입 같다면
cnt += 1
if cnt > 2: # 한 장르에 앨범 2개만 실어야해서
break
else:
answer.append(i[2]) # 노래의 고유번호 삽입
return answer
풀이1
def solution(genres, plays):
answer = []
dict1 = {}
dict2 = {}
for i, (g,p) in enumerate(zip(genres,plays)):
if g not in dict1:
dict1[g] = [(i,p)]
else:
dict1[g].append((i,p))
if g not in dict2:
dict2[g] = p
else:
dict2[g] += p
for (k,v) in sorted(dict2.items(), key = lambda x:-x[1]): # 많이 재생한 노래 타입 먼저
for (i,p) in sorted(dict1[k], key = lambda x:-x[1])[:2]: # 많이 재생한 노래 2개
answer.append(i)
return answer
풀이2
1. 속한 노래 많이 재생된 장르
2. 장르 내 가자아 많이 재생된 노래
3. 같으면 고유 번호 낮은 노래 먼저
우선순위를 생각하자.
'알고리즘 문제 풀이 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 실패율 python (0) | 2022.03.25 |
---|---|
[프로그래머스] 가장 큰 수 python (*) (0) | 2022.03.25 |
[프로그래머스] 위장 python (0) | 2022.03.25 |
[프로그래머스] 전화번호 목록 python (0) | 2022.03.25 |
[프로그래머스] 완주하지 못한 선수 python (0) | 2022.03.23 |