LeetCode-in-All

295. Find Median from Data Stream

Hard

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.

Implement the MedianFinder class:

Example 1:

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:

Follow up:

Solution

class Heap {
    private heap: number[]

    constructor() {
        this.heap = [0]
    }

    peek(): number | null {
        return this.heap[1] ?? null
    }

    push(val: number): void {
        this.heap.push(val)
        let i = this.heap.length - 1
        let parI = Math.floor(i / 2)
        while (i > 1 && this.heap[i] < this.heap[parI]) {
            const tmp = this.heap[i]
            this.heap[i] = this.heap[parI]
            this.heap[parI] = tmp
            i = parI
            parI = Math.floor(i / 2)
        }
    }

    pop(): number | null {
        if (this.heap.length == 1) {
            return null
        }
        if (this.heap.length == 2) {
            return this.heap.pop()
        }
        const res = this.heap[1]
        this.heap[1] = this.heap.pop()
        let i = 1
        while (2 * i < this.heap.length) {
            const leftChildIdx = 2 * i
            const rightChildIdx = 2 * i + 1
            if (
                rightChildIdx < this.heap.length &&
                this.heap[rightChildIdx] < this.heap[leftChildIdx] &&
                this.heap[i] > this.heap[rightChildIdx]
            ) {
                const tmp = this.heap[i]
                this.heap[i] = this.heap[rightChildIdx]
                this.heap[rightChildIdx] = tmp
                i = rightChildIdx
            } else if (this.heap[i] > this.heap[leftChildIdx]) {
                const tmp = this.heap[i]
                this.heap[i] = this.heap[leftChildIdx]
                this.heap[leftChildIdx] = tmp
                i = leftChildIdx
            } else {
                break
            }
        }
        return res
    }

    length(): number {
        return this.heap.length - 1
    }
}

class MedianFinder {
    large: Heap
    small: Heap

    constructor() {
        this.large = new Heap()
        this.small = new Heap()
    }

    addNum(num: number): void {
        if (this.small.length() === this.large.length()) {
            this.small.push(-num)
            this.large.push(-this.small.pop())
        } else {
            this.large.push(num)
            this.small.push(-this.large.pop())
        }
    }

    findMedian(): number {
        if (this.small.length() === this.large.length()) {
            return (this.large.peek() - this.small.peek()) / 2
        }
        return this.large.peek()
    }
}

/*
 * Your MedianFinder object will be instantiated and called as such:
 * var obj = new MedianFinder()
 * obj.addNum(num)
 * var param_2 = obj.findMedian()
 */

export { MedianFinder }