-
Notifications
You must be signed in to change notification settings - Fork 0
/
bestalbum.go
84 lines (70 loc) · 1.73 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"sort"
)
func solution(genres []string, plays []int) []int {
type album struct {
idx int
playCnt int
}
var bestalbum = make([]int,0)
var genresAlbum = make(map[string][]album)
var genresAlbumVal = make(map[string]int)
for i:=0; i < len(genres);i++ {
_, exists := genresAlbum[genres[i]]
if !exists{
genresAlbum[genres[i]] = make([]album,len(genres))
genresAlbum[genres[i]][i] = album{idx:i+1 , playCnt: plays[i]}
genresAlbumVal[genres[i]] = genresAlbumVal[genres[i]] + plays[i]
}else {
genresAlbum[genres[i]][i] = album{idx:i+1 , playCnt: plays[i]}
genresAlbumVal[genres[i]] = genresAlbumVal[genres[i]] + plays[i]
}
}
for _, val := range genresAlbum {
sort.Slice(val, func(i, j int) bool {
result := false;
if val[i].playCnt > val[j].playCnt {
result = true;
} else if val[i].playCnt == val[j].playCnt {
if val[i].idx < val[j].idx {
result = true
}else
{
result = false
}
}else
{
result = false
}
return result
})
}
sorted := map[int]string{}
sortedKeys := []int{}
for key, val := range genresAlbumVal {
sorted[val]=key
sortedKeys = append(sortedKeys, val)
}
sort.Sort(sort.Reverse(sort.IntSlice(sortedKeys)))
var seletedCnt = 0;
for _,val := range sortedKeys {
seletedCnt = 0;
for i:=0; i < len(genresAlbum[sorted[val]]);i++ {
if genresAlbum[sorted[val]][i].idx > 0 {
bestalbum = append(bestalbum,genresAlbum[sorted[val]][i].idx-1)
seletedCnt++
if seletedCnt >= 2 {
break
}
}
}
}
return bestalbum
}
func main() {
var genres = []string{"pop", "pop", "pop", "classic", "pop","asdf"}
var plays = []int{500, 500, 150, 800, 2500,123}
fmt.Println(solution(genres,plays))
}