-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmsic.go
80 lines (69 loc) · 1.3 KB
/
msic.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
package mselector
func map2slice(m map[int]int) []int {
tiles := []int{}
for tile, cnt := range m {
for i := 0; i < cnt; i++ {
tiles = append(tiles, tile)
}
}
return tiles
}
func slice2map(s []int) map[int]int {
m := make(map[int]int)
for _, tile := range s {
if _, exists := m[tile]; exists {
m[tile]++
} else {
m[tile] = 1
}
}
return m
}
func mergeMap(m1, m2 map[int]int) map[int]int {
for tile, cnt := range m2 {
if _, exists := m1[tile]; exists {
m1[tile] += cnt
} else {
m1[tile] = cnt
}
}
return m1
}
func getMinValueSlice(m map[int]int) ([]int, int) {
// value到keys的对应关系
valueKeys := map[int][]int{}
// 最小值
minValue := -1
for k, v := range m {
keys, exists := valueKeys[v]
if !exists {
keys = []int{k}
} else {
keys = append(keys, k)
}
valueKeys[v] = keys
if minValue == -1 || v <= minValue {
minValue = v
}
}
return valueKeys[minValue], minValue
}
func getMaxValueSlice(m map[int]int) ([]int, int) {
// value到keys的对应关系
valueKeys := map[int][]int{}
// 最大值
maxValue := -1
for k, v := range m {
keys, exists := valueKeys[v]
if !exists {
keys = []int{k}
} else {
keys = append(keys, k)
}
valueKeys[v] = keys
if v >= maxValue {
maxValue = v
}
}
return valueKeys[maxValue], maxValue
}