480. Sliding Window Median - Detailed Explanation
Problem Statement
Given an integer array nums
and a sliding window of size k
, you must move the window from the leftmost side of the array to the rightmost side. For each position of the sliding window, you need to calculate the median of the elements within the window. The median is defined as the middle element after sorting if the number of elements is odd, or the average of the two middle elements if the number of elements is even. Return an array of medians corresponding to each window position.
For example, if nums = [1,3,-1,-3,5,3,6,7]
and k = 3
, the medians for each sliding window would be:
- Window [1, 3, -1] → median is 1
- Window [3, -1, -3] → median is -1
- Window [-1, -3, 5] → median is -1
- Window [-3, 5, 3] → median is 3
- Window [5, 3, 6] → median is 5
- Window [3, 6, 7] → median is 6
Hints
-
Two Heaps Method:
To efficiently calculate the median, consider using two heaps:- A max-heap to maintain the lower half of numbers.
- A min-heap to maintain the upper half of numbers. This allows you to retrieve the middle element(s) quickly.
-
Balancing Heaps:
Keep the heaps balanced so that their sizes differ by at most one. When the window size is odd, one heap will contain one extra element, and that top element is the median. When the window size is even, the median is the average of the tops of both heaps. -
Lazy Deletion:
As the window slides, numbers leave the window. Removing an element directly from a heap is inefficient. Use a lazy deletion strategy by marking elements for removal and cleaning them up when they reach the top of the heap.
Approaches
Brute Force Approach
- Idea:
For every window, sort thek
elements and then calculate the median. - Drawback:
Sorting each window takes O(k log k) time and doing this for each window results in O(n k log k) time, which is inefficient for largen
.
Optimal Approach: Two Heaps with Lazy Deletion
- Idea:
Use a max-heap and a min-heap to keep track of the lower and upper halves of the window elements, respectively. When a new element enters the window, add it to one of the heaps (and balance if needed). When an element leaves, mark it for deletion and remove it lazily. - Steps:
-
Insertion:
Insert the incoming number into the appropriate heap. Balance the heaps if necessary. -
Removal:
Instead of directly removing the outgoing number (which can be inefficient), record its occurrence in a hash map. When the top of a heap is marked as deleted, remove it. -
Median Calculation:
Ifk
is odd, the median is the top of the max-heap. Ifk
is even, the median is the average of the tops of the max-heap and min-heap. -
Sliding the Window:
Move the window by one position, update heaps for the new element and mark the element leaving the window for lazy deletion.
-
Complexity Analysis
-
Time Complexity:
Insertion and deletion in heaps take O(log k) time. Each of then - k + 1
windows requires O(log k) operations, resulting in an overall time complexity of O(n log k). -
Space Complexity:
Two heaps store at mostk
elements, and the lazy deletion map holds at mostk
elements, resulting in O(k) space.
Python Code
Java Code
Step-by-Step Walkthrough and Visual Examples
-
Initialization:
- For the first
k
elements, insert them into the DualHeap (which maintains two heaps). - After inserting, the heaps are balanced, and the median is calculated from the tops of the heaps.
- For the first
-
Sliding the Window:
- For each subsequent element in
nums
, add the new element into the DualHeap. - Mark the element that is moving out of the window for lazy deletion and remove it eventually when it reaches the top of its heap.
- After every addition and removal, rebalance the heaps and compute the current median.
- For each subsequent element in
-
Median Calculation:
- If
k
is odd, the median is the top of the max-heap (small
), otherwise, it is the average of the tops of both heaps. - Append the median to the result list.
- If
Common Mistakes
-
Direct Heap Removal:
Removing an arbitrary element directly from a heap is inefficient. Use lazy deletion instead. -
Heap Imbalance:
Failing to balance the heaps after each operation can lead to incorrect median values. -
Delayed Map Management:
Forgetting to properly decrement or prune delayed elements can result in stale values affecting the median.
Edge Cases
-
Window Size of 1:
The median is simply each element. -
All Elements are Equal:
The median remains the same for every window. -
Negative Numbers and Mixed Signs:
Ensure that comparisons and arithmetic operations work correctly with negative values.
Alternative Variations
-
Fixed-Size Window Problems:
Similar techniques can be applied to other fixed-size window problems where order statistics are required. -
Dynamic Median Queries:
The two heaps approach can be extended to maintain a dynamic median in a stream of numbers.
Related Problems
GET YOUR FREE
Coding Questions Catalog