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
- string유형
- 알고리즘 조건
- 회전 및 자리 이동 연산
- 괄호 검사 프로그램
- 입출력 관리자
- c언어 괄호검사
- 범위 기반 for문
- 유형 변환
- C언어 덱
- 프로그래머스 배열만들기4
- l-value참조자
- 운영체제 기능
- const화
- C언어 계산기 프로그램
- 주기억장치
- 문자형 배열
- r-value참조자
- 원형 연결 구조 연결된 큐
- 값/참조/주소에 의한 전달
- getline()함수
- LAN의 분류
- 네트워크 결합
- IPv4 주소체계
- 백준 파이썬
- 문제해결 단계
- C언어 스택 연산
- 프로그래머스 푸드 파이트 대회
- auto 키워드
- const l-value참조자
- 논리 연산
Archives
- Today
- Total
chyam
[프로그래머스 Lv3, python] - 베스트앨범 본문

https://school.programmers.co.kr/learn/courses/30/lessons/42579
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
# 내가 작성한 코드
def solution(genres, plays):
answer = []
cnt_dict = {}
idx_dict = {}
for g in range(len(genres)): # (재생수,번호)로 저장
if genres[g] not in cnt_dict:
cnt_dict[genres[g]] = plays[g]
idx_dict[genres[g]] = [[plays[g],g]]
else:
cnt_dict[genres[g]] += plays[g]
idx_dict[genres[g]].append([plays[g],g])
while cnt_dict:
max_total = 0
max_g = ''
for g in cnt_dict:
if max_total < cnt_dict[g]: # 젤 큰거
max_total = cnt_dict[g]
max_g = g
del cnt_dict[max_g] # 젤 큰거 삭제
idx_dict[max_g].sort(key=lambda x:[-x[0],x[1]]) # 정렬
answer.append(idx_dict[max_g][0][1])
if len(idx_dict[max_g]) > 1:
answer.append(idx_dict[max_g][1][1])
return answer
# 다른코드
def solution(genres, plays):
answer = []
cnt_dict = {}
idx_dict = {}
# 장르별 정리
for i in range(len(genres)):
g = genres[i]
cnt_dict[g] = cnt_dict.get(g, 0) + plays[i]
idx_dict.setdefault(g, []).append((plays[i], i))
# 장르를 총 재생수 기준 정렬
sorted_genres = sorted(cnt_dict, key=lambda x: -cnt_dict[x])
# 각 장르에서 상위 2개 추출
for g in sorted_genres:
songs = sorted(idx_dict[g], key=lambda x: (-x[0], x[1]))
answer.append(songs[0][1])
if len(songs) > 1:
answer.append(songs[1][1])
return answer'프로그래머스 > LV3' 카테고리의 다른 글
| [프로그래머스 Lv3, python] - 징검다리 건너기 (0) | 2026.05.07 |
|---|---|
| [프로그래머스 Lv3, python] - 불량 사용자 (0) | 2026.05.06 |
| [프로그래머스 Lv3, python] - 기지국 설치 (2) | 2026.05.03 |
| [프로그래머스 Lv3, python] - 단어 변환 (0) | 2026.04.29 |
| [프로그래머스 Lv3,python] - 네트워크 (2) | 2026.04.27 |
