06.17에 푼 문제입니답🌷
베스트앨범
해쉬를 이용해서 푸는 문제이다.
key 값을 사용하기 위해 map을 사용했다.
function solution(genres, plays) {
var answer = [];
const genresmap= new Map()
const countmap = new Map()
genres.forEach((x,i)=>{
countmap.set(x,(countmap.get(x)||0)+plays[i])
const arr = genresmap.get(x)||[]
arr.push([i,plays[i]])
genresmap.set(x,arr)
})
const count = [...countmap].sort((a,b)=>b[1]-a[1])
count.map(genre=>{
const list = genresmap.get(genre[0])
list.sort((a,b)=>b[1]-a[1])
list.forEach((x,i)=>{
if(i<2)
answer.push(x[0])
})
})
return answer;
}