-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbestAlbum.go
47 lines (40 loc) · 1.13 KB
/
bestAlbum.go
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package programmers
import (
"sort"
)
func SolutionBestAlbum(genres []string, plays []int) []int {
// ["classic", "pop", "classic", "classic", "pop"] , [500, 600, 150, 800, 2500]
myMap := make(map[string]int)
indexArr := make([]int, len(plays))
result := []int{}
for i := 0; i < len(genres); i++ {
myMap[genres[i]] += plays[i]
indexArr[i] = i
}
// 장르별 총 재생 수에 따라 정렬
sort.Slice(indexArr, func(i, j int) bool {
if myMap[genres[indexArr[i]]] != myMap[genres[indexArr[j]]] {
return myMap[genres[indexArr[i]]] > myMap[genres[indexArr[j]]]
}
return indexArr[i] < indexArr[j]
})
// 같은 장르 내에서 재생 수에 따라 정렬
sort.SliceStable(indexArr, func(i, j int) bool {
if genres[indexArr[i]] == genres[indexArr[j]] {
if plays[indexArr[i]] != plays[indexArr[j]] {
return plays[indexArr[i]] > plays[indexArr[j]]
}
return indexArr[i] < indexArr[j]
}
return false
})
genreCountMap := make(map[string]int)
for _, index := range indexArr {
genre := genres[index]
if genreCountMap[genre] < 2 {
result = append(result, index)
genreCountMap[genre]++
}
}
return result
}