-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path42579.js
32 lines (30 loc) · 1.2 KB
/
42579.js
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
32
function solution(genres, plays) {
let genrePlayCount = {};
for(let i=0; i<genres.length; i++){
if(genrePlayCount[genres[i]]){
genrePlayCount[genres[i]].count=genrePlayCount[genres[i]].count+plays[i];
if(plays[genrePlayCount[genres[i]].index[0]]<plays[i]){
genrePlayCount[genres[i]].index[1]=genrePlayCount[genres[i]].index[0];
genrePlayCount[genres[i]].index[0]=i;
}else if(genrePlayCount[genres[i]].index.length===1){
genrePlayCount[genres[i]].index[1]=i;
}else if(plays[genrePlayCount[genres[i]].index[1]]<plays[i]){
genrePlayCount[genres[i]].index[1]=i;
}
}
else
genrePlayCount[genres[i]]={count : plays[i], index: [i]}
}
let answer=[];
const newGenrePlayCount = Object.values(genrePlayCount);
newGenrePlayCount.sort((a,b)=>{
if(a.count>=b.count) return -1;
else return 1;
})
for(let i=0; i<newGenrePlayCount.length; i++){
answer.push(newGenrePlayCount[i].index[0]);
if(newGenrePlayCount[i].index.length===2)
answer.push(newGenrePlayCount[i].index[1]);
}
return answer;
}