[LeetCode] Sliding Window Median

480. 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 examples, if arr = [2,3,4], the median is 3.
  • For examples, 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 which is moving from the very left of the array to the very right. You can only see the k numbers in the window. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
vector<double> medianSlidingWindow(vector<int>& nums, int k) {
multiset<double> window(nums.begin(), nums.begin() + k);
auto mid = next(window.begin(),(k/2));
vector<double> res {(*mid + *prev(mid ,!(k & 1))) / 2};
for(int i = k; i < nums.size(); i++) {
window.insert(nums[i]);
if(nums[i] < *mid)
mid--;

if(nums[i-k] <= *mid)
mid++;
window.erase(window.find(nums[i-k]));

res.push_back((*mid + *prev(mid ,!(k & 1))) / 2);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/21/PS/LeetCode/sliding-window-median/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.