This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathfind-median-from-data-stream.go
executable file
·97 lines (81 loc) · 1.93 KB
/
find-median-from-data-stream.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
85
86
87
88
89
90
91
92
93
94
95
96
97
package problem0295
import (
"container/heap"
)
// MedianFinder 用于寻找 Median
type MedianFinder struct {
// nums 按个数被平均分配到 left 和 right 中。left.Len() >= right.Len()
left *maxHeap
right *minHeap
}
// Constructor initialize your data structure here.
func Constructor() MedianFinder {
left := new(maxHeap)
heap.Init(left)
right := new(minHeap)
heap.Init(right)
return MedianFinder{
left: left,
right: right,
}
}
// AddNum 给 MedianFinder 添加数
func (mf *MedianFinder) AddNum(n int) {
if mf.left.Len() == mf.right.Len() {
heap.Push(mf.left, n)
} else {
heap.Push(mf.right, n)
}
if mf.right.Len() > 0 && mf.left.intHeap[0] > mf.right.intHeap[0] {
mf.left.intHeap[0], mf.right.intHeap[0] = mf.right.intHeap[0], mf.left.intHeap[0]
heap.Fix(mf.left, 0)
heap.Fix(mf.right, 0)
}
}
// FindMedian 给出 Median
func (mf *MedianFinder) FindMedian() float64 {
if mf.left.Len() == mf.right.Len() {
return float64(mf.left.intHeap[0]+mf.right.intHeap[0]) / 2
}
return float64(mf.left.intHeap[0])
}
/**
* Your MedianFinder object will be instantiated and called as such:
* obj := Constructor();
* obj.AddNum(num);
* param_2 := obj.FindMedian();
*/
// maxHeap.intHeap[0] == max(maxHeap.intHeap)
type maxHeap struct {
intHeap
}
func (h maxHeap) Less(i, j int) bool {
return h.intHeap[i] > h.intHeap[j]
}
// minHeap.intHeap[0] == min(minHeap.intHeap)
type minHeap struct {
intHeap
}
// intHeap 实现了 heap 的接口
type intHeap []int
func (h intHeap) Len() int {
return len(h)
}
func (h intHeap) Less(i, j int) bool {
return h[i] < h[j]
}
func (h intHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *intHeap) Push(x interface{}) {
// Push 使用 *h,是因为
// Push 增加了 h 的长度
*h = append(*h, x.(int))
}
func (h *intHeap) Pop() interface{} {
// Pop 使用 *h ,是因为
// Pop 减短了 h 的长度
res := (*h)[len(*h)-1]
*h = (*h)[0 : len(*h)-1]
return res
}