-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_max_heap.go
178 lines (145 loc) · 3.42 KB
/
min_max_heap.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"container/heap"
"fmt"
)
type MedianFinder struct {
minHeap *MinHeap
maxHeap *MaxHeap
}
// Implement Min Heap
type MinHeap []int
func (h MinHeap) Len() int { return len(h) }
// Logic to make it min heap is here
func (h MinHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j],h[i] }
func (h *MinHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = (*h)[0:n-1]
return x
}
func (h *MinHeap) Push(num interface{}) {
*h = append(*h, num.(int))
}
func (h *MinHeap) Peek() int {
if h.Len() < 1 {
return -1
}
minHeapHead := (*h)[0]
return minHeapHead
}
// Implement Max heap
type MaxHeap []int
func (h MaxHeap) Len() int { return len(h) }
// Logic to make it max heap is here
func (h MaxHeap) Less(i, j int) bool { return h[i] > h[j] }
func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j],h[i] }
func (h *MaxHeap) Pop() interface{} {
old := *h
n := len(old)
x := (*h)[n-1]
*h = (*h)[0:n-1]
return x
}
func (h *MaxHeap) Push(num interface{}) {
*h = append(*h, num.(int))
}
func (h *MaxHeap) Peek() int {
n := h.Len()
if n < 1 {
return -1
}
maxHeapHead := (*h)[0]
return maxHeapHead
}
/** initialize your data structure here. */
func Constructor() MedianFinder {
mf := &MedianFinder{}
minH := MinHeap{}
heap.Init(&minH)
maxH := MaxHeap{}
heap.Init(&maxH)
mf.minHeap = &minH
mf.maxHeap = &maxH
return *mf
}
func (this *MedianFinder) AddNum(num int) {
if this.maxHeap.Len() == 0 && this.minHeap.Len() == 0 {
// Since both heaps are empty add to max heap for now (we could also add to min heap if we wanted)
heap.Push(this.minHeap,num)
return
} else if this.maxHeap.Len() == 0 {
heap.Push(this.maxHeap,num)
} else {
heap.Push(this.minHeap,num)
}
// Check which heap should the current number go into
//minHead := this.minHeap.Peek()
maxHead := this.maxHeap.Peek()
if num < maxHead {
heap.Push(this.minHeap,num)
} else {
heap.Push(this.minHeap,num)
}
// Check if heaps are balanced in size (i.e don't differ in size by more than one)
if Abs(this.minHeap.Len() - this.maxHeap.Len()) > 1 {
if this.minHeap.Len() > this.maxHeap.Len() {
popped := heap.Pop(this.minHeap)
heap.Push(this.maxHeap,popped)
} else {
popped := heap.Pop(this.maxHeap)
heap.Push(this.minHeap, popped)
}
}
}
func (this *MedianFinder) FindMedian() float64 {
minHead := this.minHeap.Peek()
maxHead := this.maxHeap.Peek()
if this.minHeap.Len() == this.maxHeap.Len() {
return float64(minHead+maxHead)/2
} else if this.minHeap.Len() > this.maxHeap.Len() {
return float64(minHead)
}
return float64(maxHead)
}
func (this *MedianFinder) PrintMedian() {
fmt.Printf("\nMin Heap: %v Max Heap: %v", this.minHeap, this.maxHeap)
}
/**
* Your MedianFinder object will be instantiated and called as such:
* obj := Constructor();
* obj.AddNum(num);
* param_2 := obj.FindMedian();
*/
func main() {
mf := Constructor()
mf.AddNum(2)
mf.PrintMedian()
mf.AddNum(7)
mf.PrintMedian()
mf.AddNum(1)
mf.PrintMedian()
mf.AddNum(5)
mf.PrintMedian()
fmt.Printf("\nMedian: %v", mf.FindMedian())
/*
testMax := &MaxHeap{}
heap.Push(testMax,2)
heap.Push(testMax,1)
heap.Push(testMax,5)
fmt.Printf("\nMax peek: %d", testMax.Peek())
testMin := &MinHeap{}
heap.Push(testMin,2)
heap.Push(testMin,1)
heap.Push(testMin,5)
fmt.Printf("\nMin peek: %d", testMin.Peek())
*/
}
func Abs(x int) int {
if x < 0 {
return x * -1
}
return x
}