Sliding Window Median - Problem
Sliding Window Median is a challenging problem that combines data streaming with efficient median calculation.
Given an integer array
๐ What's a median?
โข For odd-sized windows: the middle element when sorted
โข For even-sized windows: average of the two middle elements
Example: If
โข Window [1,3,-1] โ sorted: [-1,1,3] โ median = 1
โข Window [3,-1,-3] โ sorted: [-3,-1,3] โ median = -1
โข Window [-1,-3,5] โ sorted: [-3,-1,5] โ median = -1
Your task is to return an array containing the median for each window position. This problem tests your ability to efficiently maintain sorted order in a dynamic sliding window.
Given an integer array
nums and window size k, imagine a sliding window moving from left to right across the array. At each position, you need to find the median of the k numbers currently in the window.๐ What's a median?
โข For odd-sized windows: the middle element when sorted
โข For even-sized windows: average of the two middle elements
Example: If
nums = [1,3,-1,-3,5,3,6,7] and k = 3:โข Window [1,3,-1] โ sorted: [-1,1,3] โ median = 1
โข Window [3,-1,-3] โ sorted: [-3,-1,3] โ median = -1
โข Window [-1,-3,5] โ sorted: [-3,-1,5] โ median = -1
Your task is to return an array containing the median for each window position. This problem tests your ability to efficiently maintain sorted order in a dynamic sliding window.
Input & Output
example_1.py โ Basic sliding window
$
Input:
nums = [1,3,-1,-3,5,3,6,7], k = 3
โบ
Output:
[1.0, -1.0, -1.0, 3.0, 5.0, 6.0]
๐ก Note:
Window positions and their medians: [1,3,-1]โ1, [3,-1,-3]โ-1, [-1,-3,5]โ-1, [-3,5,3]โ3, [5,3,6]โ5, [3,6,7]โ6
example_2.py โ Even-sized window
$
Input:
nums = [1,2,3,4,2,3,1,4,2], k = 4
โบ
Output:
[2.5, 3.0, 3.0, 3.0, 2.5, 3.0]
๐ก Note:
Even k means median is average of two middle elements: [1,2,3,4]โ(2+3)/2=2.5, [2,3,4,2]โ(2+3)/2=2.5, etc.
example_3.py โ Single element window
$
Input:
nums = [1,4,2,3], k = 1
โบ
Output:
[1.0, 4.0, 2.0, 3.0]
๐ก Note:
When k=1, each element is its own median: window [1]โ1, [4]โ4, [2]โ2, [3]โ3
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Balance
Set up two heaps: left side (max-heap) for smaller values, right side (min-heap) for larger values
2
Add New Element
Compare incoming element with heap tops and add to appropriate side
3
Maintain Balance
If one side gets too heavy (size difference > 1), move element to other side
4
Remove Old Element
Mark outgoing elements for lazy deletion to avoid expensive heap operations
5
Extract Median
For odd k: take top of larger heap. For even k: average both heap tops
Key Takeaway
๐ฏ Key Insight: By maintaining two balanced heaps (one for smaller half, one for larger half), we achieve O(1) median access while keeping insertion/deletion at O(log k). The balance is the secret - it ensures median elements are always at the heap tops!
Time & Space Complexity
Time Complexity
O(n log k)
n elements, each heap operation takes O(log k)
โก Linearithmic
Space Complexity
O(k)
Two heaps store at most k elements total, plus hash map for lazy deletion
โ Linear Space
Constraints
- 1 โค k โค nums.length โค 105
- -231 โค nums[i] โค 231 - 1
- Answers within 10-5 of actual value will be accepted
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code