✨ 문제 ✨

✨ 정답 ✨
function solution(genres, plays) {
const genreTotalPlays = {};
const genreSongs = {};
for (let i = 0; i < genres.length; i++) {
const genre = genres[i];
const play = plays[i];
if (!genreTotalPlays[genre]) {
genreTotalPlays[genre] = 0;
genreSongs[genre] = [];
}
genreTotalPlays[genre] += play;
genreSongs[genre].push({ index: i, play: play });
}
const sortedGenres = Object.keys(genreTotalPlays).sort((a, b) => {
return genreTotalPlays[b] - genreTotalPlays[a];
});
const answer = [];
sortedGenres.forEach(genre => {
const songs = genreSongs[genre];
songs.sort((a, b) => {
if (a.play === b.play) {
return a.index - b.index;
} else {
return b.play - a.play;
}
});
answer.push(songs[0].index);
if (songs.length > 1) {
answer.push(songs[1].index);
}
});
return answer;
}
🧵 참고한 정답지 🧵
💡💡 기억해야 할 점 💡💡