| 66 | } |
| 67 | |
| 68 | func (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 | |
| 83 | func (this *MedianFinder) FindMedian() float64 { |
| 84 | if this.smallerHalf.Len() == this.largerHalf.Len() { |