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 |
Tags
- java
- react
- maeil-mail
- useDispatch
- redux-saga
- json-server
- 이코테
- 테코테코
- 프로그래머스
- Python
- 자바
- 항해99
- programmers
- 매일메일
- 항해플러스
- react-router
- 리액트
- 알고리즘
- 코딩테스트합격자되기
- Get
- redux
- axios
- C++
- react-redux
- createSlice
- redux-toolkit
- SW
- JavaScript
- Algorithm
- sw expert academy
Archives
- Today
- Total
Binary Journey
[프로그래머스] 베스트 앨범 본문
반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
사실 조건은 4개로 보아야 함
오랜만에 +2점...!
from functools import cmp_to_key
def cmp_sum(x, y):
return y[1] - x[1]
def cmp_by_condition(x, y):
x_index, x_play = x
y_index, y_play = y
if x_play == y_play:
return x_index - y_index
else:
return y_play - x_play
def solution(genres, plays):
answer = []
_dict1 = dict()
_dict2 = dict()
for i, (genre, play) in enumerate(zip(genres, plays)) :
if genre not in _dict1.keys():
_dict1[genre] = [(i, play)]
_dict2[genre] = play
else:
_dict1[genre].append((i, play))
_dict2[genre] += play
tmp = sorted(_dict2.items(), key=cmp_to_key(cmp_sum))
for (genre, _) in tmp:
new = [x[0] for x in sorted(_dict1[genre], key=cmp_to_key(cmp_by_condition))]
answer += new[:2]
return answer
추천풀이 중 내가 생각했던 방법이랑 비슷했던 풀이
def solution(genres, plays):
answer = []
dic1 = {}
dic2 = {}
for i, (g, p) in enumerate(zip(genres, plays)):
if g not in dic1:
dic1[g] = [(i, p)]
else:
dic1[g].append((i, p))
if g not in dic2:
dic2[g] = p
else:
dic2[g] += p
for (k, v) in sorted(dic2.items(), key=lambda x:x[1], reverse=True):
for (i, p) in sorted(dic1[k], key=lambda x:x[1], reverse=True)[:2]:
answer.append(i)
return answer
반응형
'프로그래머스 > level 3' 카테고리의 다른 글
[프로그래머스] 이중우선순위큐 (0) | 2022.06.23 |
---|---|
[프로그래머스] 순위 (0) | 2022.04.21 |
[프로그래머스] 가장 먼 노드 (0) | 2022.04.21 |
[프로그래머스] 정수 삼각형 (0) | 2022.03.16 |
[프로그래머스] 헤비 유저가 소유한 장소 (MySQL) (0) | 2021.08.10 |