Last updated on January 14th, 2025 at 06:22 am
Here, we see a Find Median from Data Stream LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.
List of all LeetCode Solution
Topics
Design, Heap
Companies
Level of Question
Hard
Find Median from Data Stream LeetCode Solution
Table of Contents
1. Problem Statement
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 is3
. - For example, for
arr = [2,3]
, the median is(2 + 3) / 2 = 2.5
.
Implement the MedianFinder class:
MedianFinder()
initializes theMedianFinder
object.void addNum(int num)
adds the integernum
from the data stream to the data structure.double findMedian()
returns the median of all elements so far. Answers within10-5
of the actual answer will be accepted.
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
2. Coding Pattern Used in Solution
The code in all four languages uses the Two Heaps pattern. This pattern is commonly used to efficiently find the median of a stream of numbers. The idea is to maintain two heaps:
- A max-heap to store the smaller half of the numbers.
- A min-heap to store the larger half of the numbers.
By balancing the sizes of these heaps, the median can be efficiently calculated in constant time.
3. Code Implementation in Different Languages
3.1 Find Median from Data Stream C++
class MedianFinder { public: priority_queue<int, vector<int>, greater<int> > minHeap; priority_queue<int> maxHeap; MedianFinder(){} void addNum(int num) { if (maxHeap.empty() or maxHeap.top() > num) { maxHeap.push(num); } else { minHeap.push(num); } if (maxHeap.size() > minHeap.size() + 1) { minHeap.push(maxHeap.top()); maxHeap.pop(); } else if (minHeap.size() > maxHeap.size() + 1) { maxHeap.push(minHeap.top()); minHeap.pop(); } } double findMedian() { if (maxHeap.size() == minHeap.size()) { if (maxHeap.empty()) { return 0; } else { double avg = (maxHeap.top() + minHeap.top()) / 2.0; return avg; } } else { return maxHeap.size() > minHeap.size() ? maxHeap.top() : minHeap.top(); } } };
3.2 Find Median from Data Stream Java
class MedianFinder { PriorityQueue<Integer> min = new PriorityQueue(); PriorityQueue<Integer> max = new PriorityQueue(1000, Collections.reverseOrder()); public void addNum(int num) { max.offer(num); min.offer(max.poll()); if (max.size() < min.size()){ max.offer(min.poll()); } } public double findMedian() { if (max.size() == min.size()) return (max.peek() + min.peek()) / 2.0; else return max.peek(); } };
3.3 Find Median from Data Stream JavaScript
var MedianFinder = function() { this.maxHeap = new Heap(Heap.maxComparator); this.minHeap = new Heap(Heap.minComparator); }; MedianFinder.prototype.addNum = function(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) { this.minHeap.add(this.maxHeap.poll()); } else if(this.minHeap.size - this.maxHeap.size > 1) { this.maxHeap.add(this.minHeap.poll()); } }; MedianFinder.prototype.findMedian = function() { if(this.maxHeap.size > this.minHeap.size) { return this.maxHeap.peek(); } else if(this.maxHeap.size < this.minHeap.size) { return this.minHeap.peek(); } else { return (this.maxHeap.peek() + this.minHeap.peek()) / 2; } }; class Heap { constructor(comparator) { this.size = 0; this.values = []; this.comparator = comparator || Heap.minComparator; } add(val) { this.values.push(val); this.size ++; this.bubbleUp(); } peek() { return this.values[0] || null; } poll() { const max = this.values[0]; const end = this.values.pop(); this.size --; if (this.values.length) { this.values[0] = end; this.bubbleDown(); } return max; } bubbleUp() { let index = this.values.length - 1; let parent = Math.floor((index - 1) / 2); while (this.comparator(this.values[index], this.values[parent]) < 0) { [this.values[parent], this.values[index]] = [this.values[index], this.values[parent]]; index = parent; parent = Math.floor((index - 1) / 2); } } bubbleDown() { let index = 0, length = this.values.length; while (true) { let left = null, right = null, swap = null, leftIndex = index * 2 + 1, rightIndex = index * 2 + 2; if (leftIndex < length) { left = this.values[leftIndex]; if (this.comparator(left, this.values[index]) < 0) swap = leftIndex; } if (rightIndex < length) { right = this.values[rightIndex]; if ((swap !== null && this.comparator(right, left) < 0) || (swap === null && this.comparator(right, this.values[index]))) { swap = rightIndex; } } if (swap === null) break; [this.values[index], this.values[swap]] = [this.values[swap], this.values[index]]; index = swap; } } } Heap.minComparator = (a, b) => { return a - b; } Heap.maxComparator = (a, b) => { return b - a; }
3.4 Find Median from Data Stream Python
class MedianFinder: def __init__(self): self.small = [] self.large = [] def addNum(self, num): if len(self.small) == len(self.large): heappush(self.large, -heappushpop(self.small, -num)) else: heappush(self.small, -heappushpop(self.large, num)) def findMedian(self): if len(self.small) == len(self.large): return float(self.large[0] - self.small[0]) / 2.0 else: return float(self.large[0])
4. Time and Space Complexity
Time Complexity | Space Complexity | |
C++ | O(log N) | O(N) |
Java | O(log N) | O(N) |
JavaScript | O(log N) | O(N) |
Python | O(log N) | O(N) |
- The implementation in all four languages follows the same logic but uses language-specific constructs for heaps.
- The Two Heaps pattern ensures efficient median calculation for a dynamic stream of numbers.
- The time and space complexities are consistent across all implementations.