-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathSolution.go
62 lines (57 loc) · 950 Bytes
/
Solution.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
package Solution
func Solution(count []int) []float64 {
var _max, _min, _mode, _modeCount int
_min = -1
total := int64(0)
sum := int64(0)
for i, c := range count {
if c == 0 {
continue
}
sum += int64(i) * int64(c)
total += int64(c)
_max = max(_max, i)
if _min == -1 || _min > i {
_min = i
}
if c > _modeCount {
_modeCount = c
_mode = i
}
}
_avg := float64(sum) / float64(total)
var a float64
start := total / 2
if total&1 == 0 {
start--
}
used := int64(0)
justSelected := false
for i, c := range count {
if c == 0 {
continue
}
if justSelected {
a += float64(i)
a /= 2
break
}
next := used + int64(c)
if next-1 < start {
used = next
continue
}
diff := next - start
a += float64(i)
if total&1 == 1 {
break
}
if diff >= 2 {
a += a
a /= 2
break
}
justSelected = true
}
return []float64{float64(_min), float64(_max), _avg, a, float64(_mode)}
}