Sliding Window Median
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, so the median is the mean of the two middle values.
- For example, if
arr = [2,3,4], the median is3. - For example, if
arr = [1,2,3,4], the median is(2 + 3) / 2 = 2.5.
You are given an integer array nums and an integer k. There is a sliding window of size k that moves from the very left of the array to the very right. You can only see the k numbers in the window, and each time the sliding window moves right by one position.
Return the median array for each window in the original array. Answers within 10^-5 of the actual value will be accepted.
Example 1
Input
nums = [1,3,-1,-3,5,3,6,7], k = 3Output
[1,-1,-1,3,5,6]The medians of the six size-3 windows are 1, -1, -1, 3, 5, and 6.
Example 2
Input
nums = [1,2,3,4,2,3,1,4,2], k = 3Output
[2,3,3,3,2,3,2]Each output value is the median of the corresponding contiguous window of length 3.
Constraints
- 1 <= k <= nums.length <= 10^5
- -2^31 <= nums[i] <= 2^31 - 1