-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path295_find_median_from_data_streams.js
161 lines (142 loc) · 4.48 KB
/
295_find_median_from_data_streams.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
class MedianFinder {
constructor() {
this.lowerHalf = new MaxHeap();
this.upperHalf = new MinHeap();
}
addNum(num) {
if (this.lowerHalf.isEmpty() || num <= this.lowerHalf.peek()) {
this.lowerHalf.push(num);
} else {
this.upperHalf.push(num);
}
// Rebalance the heaps if necessary
if (this.lowerHalf.size() > this.upperHalf.size() + 1) {
this.upperHalf.push(this.lowerHalf.pop());
} else if (this.upperHalf.size() > this.lowerHalf.size()) {
this.lowerHalf.push(this.upperHalf.pop());
}
}
findMedian() {
if (this.lowerHalf.size() === this.upperHalf.size()) {
return (this.lowerHalf.peek() + this.upperHalf.peek()) / 2;
} else {
return this.lowerHalf.peek();
}
}
}
class MaxHeap {
constructor() {
this.heap = [];
}
push(value) {
this.heap.push(value);
this.heapifyUp();
}
pop() {
if (this.isEmpty()) return null;
const maxElement = this.heap[0];
this.heap[0] = this.heap[this.heap.length - 1];
this.heap.pop();
this.heapifyDown();
return maxElement;
}
peek() {
return this.isEmpty() ? null : this.heap[0];
}
size() {
return this.heap.length;
}
isEmpty() {
return this.heap.length === 0;
}
heapifyUp() {
let currentIdx = this.heap.length - 1;
while (currentIdx > 0) {
const parentIdx = Math.floor((currentIdx - 1) / 2);
if (this.heap[currentIdx] > this.heap[parentIdx]) {
[this.heap[currentIdx], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[currentIdx]];
currentIdx = parentIdx;
} else {
break;
}
}
}
heapifyDown() {
let currentIdx = 0;
while (currentIdx < this.heap.length) {
let maxIdx = currentIdx;
const leftChildIdx = currentIdx * 2 + 1;
const rightChildIdx = currentIdx * 2 + 2;
if (leftChildIdx < this.heap.length && this.heap[leftChildIdx] > this.heap[maxIdx]) {
maxIdx = leftChildIdx;
}
if (rightChildIdx < this.heap.length && this.heap[rightChildIdx] > this.heap[maxIdx]) {
maxIdx = rightChildIdx;
}
if (maxIdx !== currentIdx) {
[this.heap[currentIdx], this.heap[maxIdx]] = [this.heap[maxIdx], this.heap[currentIdx]];
currentIdx = maxIdx;
} else {
break;
}
}
}
}
class MinHeap {
constructor() {
this.heap = [];
}
push(value) {
this.heap.push(value);
this.heapifyUp();
}
pop() {
if (this.isEmpty()) return null;
const minElement = this.heap[0];
this.heap[0] = this.heap[this.heap.length - 1];
this.heap.pop();
this.heapifyDown();
return minElement;
}
peek() {
return this.isEmpty() ? null : this.heap[0];
}
size() {
return this.heap.length;
}
isEmpty() {
return this.heap.length === 0;
}
heapifyUp() {
let currentIdx = this.heap.length - 1;
while (currentIdx > 0) {
const parentIdx = Math.floor((currentIdx - 1) / 2);
if (this.heap[currentIdx] < this.heap[parentIdx]) {
[this.heap[currentIdx], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[currentIdx]];
currentIdx = parentIdx;
} else {
break;
}
}
}
heapifyDown() {
let currentIdx = 0;
while (currentIdx < this.heap.length) {
let minIdx = currentIdx;
const leftChildIdx = currentIdx * 2 + 1;
const rightChildIdx = currentIdx * 2 + 2;
if (leftChildIdx < this.heap.length && this.heap[leftChildIdx] < this.heap[minIdx]) {
minIdx = leftChildIdx;
}
if (rightChildIdx < this.heap.length && this.heap[rightChildIdx] < this.heap[minIdx]) {
minIdx = rightChildIdx;
}
if (minIdx !== currentIdx) {
[this.heap[currentIdx], this.heap[minIdx]] = [this.heap[minIdx], this.heap[currentIdx]];
currentIdx = minIdx;
} else {
break;
}
}
}
}