MCPcopy
hub / github.com/austingebauer/go-leetcode / AddNum

Method AddNum

find_median_from_data_stream_295/solution.go:68–81  ·  view source on GitHub ↗
(num int)

Source from the content-addressed store, hash-verified

66}
67
68func (this *MedianFinder) AddNum(num int) {
69 // push num onto the max heap for the smaller half of numbers
70 heap.Push(this.smallerHalf, num)
71
72 // to maintain balance between the two heaps, offer
73 // largest from smaller half to the larger half
74 heap.Push(this.largerHalf, heap.Pop(this.smallerHalf).(int))
75
76 // if the larger half becomes larger in length that the smaller half,
77 // we need to offer the lowest in the largest half to the smaller half
78 if this.largerHalf.Len() > this.smallerHalf.Len() {
79 heap.Push(this.smallerHalf, heap.Pop(this.largerHalf).(int))
80 }
81}
82
83func (this *MedianFinder) FindMedian() float64 {
84 if this.smallerHalf.Len() == this.largerHalf.Len() {

Callers 2

TestMedianFinder_0Function · 0.80
TestMedianFinder_1Function · 0.80

Calls 3

PushMethod · 0.45
PopMethod · 0.45
LenMethod · 0.45

Tested by 2

TestMedianFinder_0Function · 0.64
TestMedianFinder_1Function · 0.64