-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1619.go
52 lines (48 loc) · 949 Bytes
/
p1619.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
package leetcode
import (
"github.com/emirpasic/gods/trees/binaryheap"
"github.com/emirpasic/gods/utils"
)
func trimMean(arr []int) float64 {
l := len(arr)
n := l / 20
// 小顶堆存较大的数
heapMin := binaryheap.NewWithIntComparator()
// 大顶堆存较小的树
heapMax := binaryheap.NewWith(func(a, b interface{}) int {
return utils.IntComparator(b, a)
})
sum := 0
for _, v := range arr {
// 较小的数
if heapMax.Size() < n {
heapMax.Push(v)
} else {
v1, _ := heapMax.Peek()
if v1.(int) > v {
heapMax.Pop()
heapMax.Push(v)
}
}
// 较大的数
if heapMin.Size() < n {
heapMin.Push(v)
} else {
v1, _ := heapMin.Peek()
if v1.(int) < v {
heapMin.Pop()
heapMin.Push(v)
}
}
sum += v
}
it := heapMax.Iterator()
for it.Next() {
sum -= it.Value().(int)
}
it = heapMin.Iterator()
for it.Next() {
sum -= it.Value().(int)
}
return float64(sum) / float64(l-n-n)
}