|
| 1 | +/** |
| 2 | + * https://leetcode.com/problems/find-median-from-data-stream |
| 3 | + * array with binary search |
| 4 | + */ |
| 5 | +class MedianFinder { |
| 6 | + // S.C. O(n) |
| 7 | + private nums: number[] = []; |
| 8 | + |
| 9 | + // T.C. O(n) |
| 10 | + addNum(num: number): void { |
| 11 | + if (this.nums.length === 0) { |
| 12 | + this.nums.push(num); |
| 13 | + return; |
| 14 | + } else { |
| 15 | + this.putNumWithBinarySearch(num); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + private putNumWithBinarySearch(num: number): void { |
| 20 | + let left = 0; |
| 21 | + let right = this.nums.length - 1; |
| 22 | + |
| 23 | + while (left <= right) { |
| 24 | + const mid = Math.floor((left + right) / 2); |
| 25 | + if (this.nums[mid] === num) { |
| 26 | + this.nums.splice(mid, 0, num); |
| 27 | + return; |
| 28 | + } else if (this.nums[mid] < num) { |
| 29 | + left = mid + 1; |
| 30 | + } else { |
| 31 | + right = mid - 1; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // T.C. O(n) |
| 36 | + this.nums.splice(left, 0, num); |
| 37 | + } |
| 38 | + |
| 39 | + // T.C. O(1) |
| 40 | + findMedian(): number { |
| 41 | + const len = this.nums.length; |
| 42 | + |
| 43 | + if (len % 2 === 1) { |
| 44 | + return this.nums[Math.floor(len / 2)]; |
| 45 | + } else { |
| 46 | + return (this.nums[len / 2] + this.nums[len / 2 - 1]) / 2; |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * heap... |
| 53 | + * TL;DR |
| 54 | + */ |
| 55 | +class MedianFinder { |
| 56 | + // S.C. O(n) |
| 57 | + private smallHalf: MaxHeap = new MaxHeap(); |
| 58 | + private largeHalf: MinHeap = new MinHeap(); |
| 59 | + |
| 60 | + // T.C. O(log n) |
| 61 | + addNum(num: number): void { |
| 62 | + this.smallHalf.push(num); |
| 63 | + this.largeHalf.push(this.smallHalf.pop()!); |
| 64 | + |
| 65 | + if (this.smallHalf.size() < this.largeHalf.size()) { |
| 66 | + this.smallHalf.push(this.largeHalf.pop()!); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // T.C. O(1) |
| 71 | + findMedian(): number { |
| 72 | + if (this.smallHalf.size() === this.largeHalf.size()) { |
| 73 | + return (this.smallHalf.peek()! + this.largeHalf.peek()!) / 2; |
| 74 | + } else { |
| 75 | + return this.smallHalf.peek()!; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +class MinHeap { |
| 81 | + protected heap: number[] = []; |
| 82 | + |
| 83 | + push(val: number): void { |
| 84 | + this.heap.push(val); |
| 85 | + this.heapifyUp(); |
| 86 | + } |
| 87 | + |
| 88 | + pop(): number | undefined { |
| 89 | + if (this.heap.length === 0) return undefined; |
| 90 | + if (this.heap.length === 1) return this.heap.pop(); |
| 91 | + |
| 92 | + const result = this.heap[0]; |
| 93 | + this.heap[0] = this.heap.pop()!; |
| 94 | + this.heapifyDown(); |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + peek(): number | undefined { |
| 99 | + return this.heap[0]; |
| 100 | + } |
| 101 | + |
| 102 | + size(): number { |
| 103 | + return this.heap.length; |
| 104 | + } |
| 105 | + |
| 106 | + private heapifyUp(): void { |
| 107 | + let index = this.heap.length - 1; |
| 108 | + while (index > 0) { |
| 109 | + const parentIndex = Math.floor((index - 1) / 2); |
| 110 | + if (this.heap[parentIndex] <= this.heap[index]) break; |
| 111 | + |
| 112 | + this.swap(parentIndex, index); |
| 113 | + index = parentIndex; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private heapifyDown(): void { |
| 118 | + let index = 0; |
| 119 | + while (true) { |
| 120 | + let smallest = index; |
| 121 | + const leftChild = 2 * index + 1; |
| 122 | + const rightChild = 2 * index + 2; |
| 123 | + |
| 124 | + if ( |
| 125 | + leftChild < this.heap.length && |
| 126 | + this.heap[leftChild] < this.heap[smallest] |
| 127 | + ) { |
| 128 | + smallest = leftChild; |
| 129 | + } |
| 130 | + |
| 131 | + if ( |
| 132 | + rightChild < this.heap.length && |
| 133 | + this.heap[rightChild] < this.heap[smallest] |
| 134 | + ) { |
| 135 | + smallest = rightChild; |
| 136 | + } |
| 137 | + |
| 138 | + if (smallest === index) break; |
| 139 | + |
| 140 | + this.swap(index, smallest); |
| 141 | + index = smallest; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + protected swap(i: number, j: number): void { |
| 146 | + [this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]]; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +class MaxHeap extends MinHeap { |
| 151 | + push(val: number): void { |
| 152 | + super.push(-val); |
| 153 | + } |
| 154 | + |
| 155 | + pop(): number | undefined { |
| 156 | + const val = super.pop(); |
| 157 | + return val === undefined ? undefined : -val; |
| 158 | + } |
| 159 | + |
| 160 | + peek(): number | undefined { |
| 161 | + const val = super.peek(); |
| 162 | + return val === undefined ? undefined : -val; |
| 163 | + } |
| 164 | +} |
0 commit comments