-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeaps_Find_the_Running_Median.swift
225 lines (181 loc) · 5.88 KB
/
Heaps_Find_the_Running_Median.swift
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
platform: HackerRank
link: https://www.hackerrank.com/challenges/ctci-find-the-running-median/problem
*/
// solution #1
protocol Heap {
var items: [Int] { get set }
var index: Int { get set }
var size: Int { get }
var leftChildIndex: Int { get }
var rightChildIndex: Int { get }
var parentIndex: Int { get }
var hasParent: Bool { get }
var hasRightChild: Bool { get }
var hasLeftChild: Bool { get }
func add(_ item: Int)
func heapifyUp()
func poll() -> Int
func heapifyDown()
func peek() -> Int
}
class MinHeap: Heap {
var items: [Int] = []
var index = 0
var size: Int { items.count }
var leftChildIndex: Int { (index * 2) + 1 }
var rightChildIndex: Int { (index * 2) + 2 }
var parentIndex: Int { (index - 1) / 2 }
var hasParent: Bool { parentIndex >= 0 }
var hasRightChild: Bool { rightChildIndex < size }
var hasLeftChild: Bool { leftChildIndex < size }
// add element to heap
func add(_ item: Int) {
items.append(item)
heapifyUp()
}
func heapifyUp () {
index = size - 1
while (hasParent && items[parentIndex] > items[index]) {
swap(index, parentIndex)
index = parentIndex
}
}
// remove minimum element
@discardableResult
func poll() -> Int {
swap(0, size - 1)
let removedMinimum = items.removeLast()
heapifyDown()
return removedMinimum
}
@discardableResult
func peek() -> Int {
if size == 0 { fatalError() }
return items[0]
}
func heapifyDown() {
index = 0
while (hasLeftChild) {
var smallerChildIndex = leftChildIndex
if hasRightChild && items[rightChildIndex] < items[leftChildIndex] {
smallerChildIndex = rightChildIndex
}
if items[index] < items[smallerChildIndex] {
break;
} else {
swap(index, smallerChildIndex)
index = smallerChildIndex
}
}
}
func swap(_ firstIndex: Int, _ secondIndex: Int) {
let temp = items[firstIndex]
items[firstIndex] = items[secondIndex]
items[secondIndex] = temp
}
}
class MaxHeap: Heap {
var items: [Int]
var index = 0
var size: Int {
items.count
}
init(items: [Int] = []) {
self.items = items
}
var leftChildIndex: Int { (index * 2) + 1 }
var rightChildIndex: Int { (index * 2) + 2 }
var parentIndex: Int { (index - 1) / 2 }
var hasLeftChild: Bool { leftChildIndex < size }
var hasRightChild: Bool { rightChildIndex < size }
var hasParent: Bool { parentIndex >= 0 }
func add(_ item: Int) {
items.append(item)
heapifyUp()
}
func heapifyUp() {
index = size - 1
while hasParent && items[parentIndex] < items[index] {
swap(index, parentIndex)
index = parentIndex
}
}
func poll() -> Int {
swap(0, size - 1)
let removedMaximum = items.removeLast()
heapifyDown()
return removedMaximum
}
func peek() -> Int {
if size == 0 { fatalError() }
return items[0]
}
func heapifyDown() {
index = 0
while hasLeftChild {
var largerChildIndex = leftChildIndex
if hasRightChild && items[leftChildIndex] < items[rightChildIndex] {
largerChildIndex = rightChildIndex
}
if items[index] > items[largerChildIndex] {
break;
} else {
swap(largerChildIndex, index)
index = largerChildIndex
}
}
}
func swap(_ firstIndex: Int, _ secondIndex: Int) {
let temp = items[firstIndex]
items[firstIndex] = items[secondIndex]
items[secondIndex] = temp
}
}
func runningMedian(arr: [Int]) -> Void {
// Print your answer within the function
let lowers = MaxHeap()
let highers = MinHeap()
for i in 0..<arr.count {
addNumber(arr[i], lowers, highers)
rebalance(lowers, highers)
print(getMedian(lowers, highers))
}
}
func addNumber(_ number: Int, _ lowers: MaxHeap, _ highers: MinHeap) {
if (lowers.size == 0 || number < lowers.peek()) {
lowers.add(number)
} else {
highers.add(number)
}
}
func rebalance(_ lowers: Heap, _ highers: Heap) {
let largerHeap: Heap = lowers.size > highers.size ? lowers : highers
let smallerHeap: Heap = lowers.size > highers.size ? highers : lowers
if largerHeap.size - smallerHeap.size >= 2 {
smallerHeap.add(largerHeap.poll())
}
}
func getMedian(_ lowers: MaxHeap, _ highers: MinHeap) -> Float {
let largerHeap: Heap = lowers.size > highers.size ? lowers : highers
let smallerHeap: Heap = lowers.size > highers.size ? highers : lowers
if largerHeap.size == smallerHeap.size {
return (Float(lowers.peek()) + Float(highers.peek())) / 2
} else {
return Float(largerHeap.peek())
}
}
runningMedian(arr: [12,4,5,3,8,7])
// 12.0
// 8.0
// 5.0
// 4.5
// 5.0
// 6.0
/*
How I approached this problem:
Heap을 활용. 중간 값 기준 아래의 값들을 maxHeap 에 두고, 기준 위의 값들을 minHeap에 두어서 요소가 짝수 갯수일 때는 각각의 root element의 평균을 중간값으로 반환하고, 요소가 홀수 갯수일 때는 기준 위의 minHeap의 root 엘리먼트를 중간값으로 반환한다.
배열에 요소가 추가될 때마다 heap 에 추가하고, 밸런스하고, 중간값을 구한다.
Interesting part:
스위프트에는 따로 heap이 없어서 만들어서 썼다. 이번 경험을 바탕으로 Heap 에 대한 좋은 공부가 되었다.
*/