프로그래머스 - 베스트앨범

이서현·2021년 6월 17일
0

Algorithm

목록 보기
39/76

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;
}
profile
안녕하세요. 이서현입니다( ღ'ᴗ'ღ )

0개의 댓글