오늘 푼 문제
2. 베스트앨범 / Lv.3 / 시간 : 36분
programmers.co.kr/learn/courses/30/lessons/42579?language=javascript
코딩테스트 연습 - 베스트앨범
스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다. 속한 노래가
programmers.co.kr
function solution(genres, plays) {
const hash = genres.reduce((acc,genre,index)=>{
acc[genre] = acc[genre] || {total : 0, maxIndex : null, secondIndex : null};
acc[genre].total += plays[index];
if(plays[index] > (plays[acc[genre].maxIndex] || 0)){
acc[genre].secondIndex = acc[genre].maxIndex;
acc[genre].maxIndex = index;
}else if(plays[index] > (plays[acc[genre].secondIndex]||0)){
acc[genre].secondIndex = index;
}
return acc;
},{});
const sorted = Object.values(hash).sort((a,b)=>b.total-a.total);
const answer = sorted.reduce((acc,cur)=>{
acc.push(cur.maxIndex);
if(cur.secondIndex!==null) acc.push(cur.secondIndex);
return acc;
},[]);
return answer;
}
'알고리즘 문제풀이' 카테고리의 다른 글
[프로그래머스] 고득점 키트 - 스택/큐2 (0) | 2020.10.28 |
---|---|
[프로그래머스] 고득점 키트 - 스택/큐1 (0) | 2020.10.27 |
[프로그래머스] 고득점 키트 - 해시1 (0) | 2020.10.12 |
[프로그래머스] 고득점 키트 - 정렬3 (0) | 2020.09.22 |
[프로그래머스] 고득점 키트 - 정렬1,2 (0) | 2020.09.19 |