Sliding Window Median

Given an integer array and a window size k, return the median of every contiguous window. This requires keeping the window balanced while elements enter and leave, which is why two heaps plus lazy deletion are commonly used.

Input Format

nums = array of numbers, k = window size

Output Format

median of every window in order

Constraints

  • 1 <= k <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

Example 1:

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]

Explanation:

This is the standard sliding window median example.

Example 2:

Input:

nums = [1,2]
k = 1

Output:

[1.0,2.0]

Explanation:

A window of size 1 always has median equal to the element itself.

Loading...
Sliding Window Median - Heap DSA Problem