-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-median-from-data-stream.js
169 lines (137 loc) · 4.4 KB
/
find-median-from-data-stream.js
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
/*
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.
For example, for arr = [2,3,4], the median is 3.
For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5.
Implement the MedianFinder class:
MedianFinder() initializes the MedianFinder object.
void addNum(int num) adds the integer num from the data stream to the data structure.
double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted.
--- Examples
Input
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
[[], [1], [2], [], [3], []]
Output
[null, null, null, 1.5, null, 2.0]
Explanation
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1); // arr = [1]
medianFinder.addNum(2); // arr = [1, 2]
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
medianFinder.addNum(3); // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0
--- Constraints
-105 <= num <= 105
There will be at least one element in the data structure before calling findMedian.
At most 5 * 104 calls will be made to addNum and findMedian.
--- Follow Up
If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
If 99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?
*/
class MedianFinder {
constructor() {
this.maxHeap = new BinaryHeap(true);
this.minHeap = new BinaryHeap(false);
}
addNum(num) {
if (this.maxHeap.peek() === null || num < this.maxHeap.peek()) {
this.maxHeap.add(num);
} else {
this.minHeap.add(num);
}
if (this.maxHeap.size - this.minHeap.size > 1) {
const max = this.maxHeap.poll();
this.minHeap.add(max);
} else if (this.minHeap.size - this.maxHeap.size > 1) {
const min = this.minHeap.poll();
this.maxHeap.add(min);
}
}
findMedian() {
if (!this.maxHeap.size && !this.minHeap.size) return null;
if (this.maxHeap.size === this.minHeap.size) {
return (this.maxHeap.peek() + this.minHeap.peek()) / 2;
}
if (this.maxHeap.size < this.minHeap.size) {
return this.minHeap.peek();
}
return this.maxHeap.peek();
}
}
class BinaryHeap {
constructor(isMaxHeap) {
this.values = [];
this.compare = isMaxHeap ? this.maxCompare : this.minCompare;
}
get size() {
return this.values.length;
}
minCompare(index1, index2) {
return this.values[index1] - this.values[index2];
}
maxCompare(index1, index2) {
return this.values[index2] - this.values[index1];
}
add(value) {
this.values.push(value);
this.bubbleUp();
}
peek() {
return this.values[0] || null;
}
poll() {
if (this.values.length <= 1) return this.values.pop();
const root = this.values[0];
const end = this.values.pop();
this.values[0] = end;
this.bubbleDown();
return root;
}
bubbleUp() {
let index = this.values.length - 1;
let parentIndex = this.getParentIndex(index);
while (this.compare(index, parentIndex) < 0) {
this.swap(index, parentIndex);
index = parentIndex;
parentIndex = this.getParentIndex(index);
}
}
bubbleDown() {
let index = 0;
while (true) {
let swapIndex = null;
let leftIndex = this.getLeftChildIndex(index);
let rightIndex = this.getRightChildIndex(index);
if (leftIndex && this.compare(leftIndex, index) < 0) {
swapIndex = leftIndex;
}
if (
rightIndex &&
((swapIndex === null && this.compare(rightIndex, index) < 0) ||
(swapIndex && this.compare(rightIndex, leftIndex) < 0))
) {
swapIndex = rightIndex;
}
if (!swapIndex) break;
this.swap(index, swapIndex);
index = swapIndex;
}
}
getLeftChildIndex(index) {
const leftChildIndex = index * 2 + 1;
return leftChildIndex >= this.values.length ? null : leftChildIndex;
}
getRightChildIndex(index) {
const rightChildIndex = index * 2 + 2;
return rightChildIndex >= this.values.length ? null : rightChildIndex;
}
getParentIndex(index) {
const parentIndex = Math.floor((index - 1) / 2);
return parentIndex < 0 ? null : parentIndex;
}
swap(index1, index2) {
[this.values[index1], this.values[index2]] = [
this.values[index2],
this.values[index1],
];
}
}