|
1 |
| -// Time complexity: O(n) |
| 1 | +// last test case failed |
| 2 | +// Time complexity: O(logn) |
2 | 3 | // Space complexity: O(n)
|
3 | 4 |
|
| 5 | +class MinHeap { |
| 6 | + constructor() { |
| 7 | + this.heap = [null]; |
| 8 | + } |
| 9 | + |
| 10 | + get root() { |
| 11 | + if (this.length === 1) { |
| 12 | + return null; |
| 13 | + } |
| 14 | + |
| 15 | + return this.heap[1]; |
| 16 | + } |
| 17 | + |
| 18 | + get length() { |
| 19 | + return this.heap.length; |
| 20 | + } |
| 21 | + |
| 22 | + push(value) { |
| 23 | + this.heap.push(value); |
| 24 | + |
| 25 | + let current = this.heap.length - 1; |
| 26 | + let parent = Math.floor(current / 2); |
| 27 | + |
| 28 | + while (parent && this.heap[current] < this.heap[parent]) { |
| 29 | + [this.heap[current], this.heap[parent]] = [ |
| 30 | + this.heap[parent], |
| 31 | + this.heap[current], |
| 32 | + ]; |
| 33 | + current = parent; |
| 34 | + parent = Math.floor(current / 2); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + pop() { |
| 39 | + if (this.heap.length === 1) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + if (this.heap.length === 2) { |
| 44 | + return this.heap.pop(); |
| 45 | + } |
| 46 | + |
| 47 | + const rv = this.heap[1]; |
| 48 | + this.heap[1] = this.heap.pop(); |
| 49 | + |
| 50 | + let current = 1; |
| 51 | + let left = current * 2; |
| 52 | + let right = left + 1; |
| 53 | + |
| 54 | + while ( |
| 55 | + (this.heap[left] && this.heap[current] > this.heap[left]) || |
| 56 | + (this.heap[right] && this.heap[current] > this.heap[right]) |
| 57 | + ) { |
| 58 | + if (this.heap[right] && this.heap[right] < this.heap[left]) { |
| 59 | + [this.heap[right], this.heap[current]] = [ |
| 60 | + this.heap[current], |
| 61 | + this.heap[right], |
| 62 | + ]; |
| 63 | + current = right; |
| 64 | + } else { |
| 65 | + [this.heap[left], this.heap[current]] = [ |
| 66 | + this.heap[current], |
| 67 | + this.heap[left], |
| 68 | + ]; |
| 69 | + current = left; |
| 70 | + } |
| 71 | + |
| 72 | + left = current * 2; |
| 73 | + right = left + 1; |
| 74 | + } |
| 75 | + |
| 76 | + return rv; |
| 77 | + } |
| 78 | +} |
| 79 | + |
4 | 80 | var MedianFinder = function () {
|
5 |
| - this.arr = []; |
| 81 | + this.leftMinHeap = new MinHeap(); |
| 82 | + this.rightMinHeap = new MinHeap(); |
6 | 83 | };
|
7 | 84 |
|
8 | 85 | /**
|
9 | 86 | * @param {number} num
|
10 | 87 | * @return {void}
|
11 | 88 | */
|
12 | 89 | MedianFinder.prototype.addNum = function (num) {
|
13 |
| - if (this.arr.length === 0) { |
14 |
| - this.arr.push(num); |
15 |
| - return; |
16 |
| - } |
| 90 | + const rightMinValue = this.rightMinHeap.root; |
17 | 91 |
|
18 |
| - let left = 0; |
19 |
| - let right = this.arr.length - 1; |
20 |
| - |
21 |
| - while (left <= right) { |
22 |
| - const mid = Math.floor((left + right) / 2); |
23 |
| - |
24 |
| - if (this.arr[mid] > num) { |
25 |
| - right = mid - 1; |
26 |
| - continue; |
27 |
| - } |
| 92 | + if (num >= rightMinValue) { |
| 93 | + this.rightMinHeap.push(num); |
| 94 | + } else { |
| 95 | + this.leftMinHeap.push(num * -1); |
| 96 | + } |
28 | 97 |
|
29 |
| - left = mid + 1; |
| 98 | + if (this.rightMinHeap.length - this.leftMinHeap.length > 1) { |
| 99 | + const popped = this.rightMinHeap.pop(); |
| 100 | + this.leftMinHeap.push(popped * -1); |
30 | 101 | }
|
31 | 102 |
|
32 |
| - // insert in left |
33 |
| - const sliced = this.arr.slice(left); |
34 |
| - this.arr = [...this.arr.slice(0, left), num, ...sliced]; |
| 103 | + if (this.leftMinHeap.length - this.rightMinHeap.length > 1) { |
| 104 | + const popped = this.leftMinHeap.pop(); |
| 105 | + this.rightMinHeap.push(popped * -1); |
| 106 | + } |
35 | 107 | };
|
36 | 108 |
|
37 | 109 | /**
|
38 | 110 | * @return {number}
|
39 | 111 | */
|
40 | 112 | MedianFinder.prototype.findMedian = function () {
|
41 |
| - if (this.arr.length % 2 === 0) { |
42 |
| - return ( |
43 |
| - (this.arr[this.arr.length / 2] + this.arr[this.arr.length / 2 - 1]) / 2 |
44 |
| - ); |
| 113 | + const len = this.leftMinHeap.length + this.rightMinHeap.length; |
| 114 | + |
| 115 | + if (len % 2 === 0) { |
| 116 | + return (this.leftMinHeap.root * -1 + this.rightMinHeap.root) / 2; |
| 117 | + } |
| 118 | + |
| 119 | + if (this.leftMinHeap.length > this.rightMinHeap.length) { |
| 120 | + return this.leftMinHeap.root * -1; |
45 | 121 | }
|
46 | 122 |
|
47 |
| - return this.arr[(this.arr.length - 1) / 2]; |
| 123 | + return this.rightMinHeap.root; |
48 | 124 | };
|
49 | 125 |
|
50 | 126 | /**
|
|
0 commit comments