Advertisement

Find Median from Data Stream - LeetCode 295 Solution

Find Median from Data Stream - Complete Solution Guide

Find Median from Data Stream is LeetCode problem 295, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 is 3 . For example, for arr = [2,3] , the median is (2 + 3) / 2 = 2.5 . Implement the MedianFinder class: MedianFinder() initializes the MedianFinder object. void addNum(int num) adds the integer num from the data stream to the data structure. double findMedian() returns the median of

Detailed Explanation

The problem requires designing a `MedianFinder` class that efficiently calculates the median of a stream of numbers. The `addNum(int num)` method adds a new number to the data structure, and the `findMedian()` method returns the median of all the numbers added so far. If the number of elements is odd, the median is the middle element. If the number of elements is even, the median is the average of the two middle elements. The solution needs to handle a large number of calls to `addNum` and `findMedian` efficiently.

Solution Approach

The solution employs two heaps: `small_half` (a max-heap) and `large_half` (a min-heap). `small_half` stores the smaller half of the numbers seen so far, and `large_half` stores the larger half. The algorithm maintains the invariant that all elements in `small_half` are less than or equal to all elements in `large_half`. When a new number is added, it's initially added to `small_half`. Then, the largest element of `small_half` is moved to `large_half`. After that, the sizes of the two heaps are balanced so that `small_half` either has the same number of elements as `large_half` or one more element. The median is then found by either returning the top element of `small_half` if the total number of elements is odd, or the average of the top elements of `small_half` and `large_half` if the total number of elements is even.

Step-by-Step Algorithm

  1. Step 1: Initialize two priority queues (heaps): `small_half` as a max-heap and `large_half` as a min-heap.
  2. Step 2: `addNum(num)`: Add the new number to the `small_half` (max-heap).
  3. Step 3: Move the largest element from `small_half` to `large_half` to maintain the ordering property.
  4. Step 4: Balance the heaps. If the `large_half` is larger than `small_half`, move the smallest element from `large_half` to `small_half`.
  5. Step 5: `findMedian()`: If `small_half` has more elements, the median is the top of `small_half`. Otherwise, the median is the average of the top elements of `small_half` and `large_half`.

Key Insights

  • Insight 1: Using two heaps (a max-heap and a min-heap) allows efficient tracking of the smaller and larger halves of the data stream, enabling quick median calculation.
  • Insight 2: Maintaining a balanced state between the two heaps, where the max-heap contains the smaller half of the numbers and the min-heap contains the larger half, is crucial for determining the median in O(1) time.
  • Insight 3: The sizes of the two heaps should be either equal or the max-heap (smaller half) has one more element. This property guarantees correct median calculation.

Complexity Analysis

Time Complexity: O(log n)

Space Complexity: O(n)

Topics

This problem involves: Two Pointers, Design, Sorting, Heap (Priority Queue), Data Stream.

Companies

Asked at: Agoda, Anduril, Citadel, Cohesity, Coupang, Docusign, Goldman Sachs, Google, IXL, KLA, Nvidia, Okta, PayPal, Pinterest, Salesforce, Samsung, Snowflake, Splunk, Spotify, StackAdapt, Tinder, Twitch, WorldQuant.