Home Sliding Window Median
Post
Cancel

LeetCode #480: Sliding Window Median (C/C++).

hard

source: https://leetcode.com/problems/sliding-window-median
C/C++ Solution to LeetCode problem 480. Sliding Window Median.

Problem


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.

Examples


Example 1:

Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
Explanation:

1
2
3
4
5
6
7
8
Window position                Median  
---------------                -----  
[1  3  -1] -3  5  3  6  7        1  
 1 [3  -1  -3] 5  3  6  7       -1  
 1  3 [-1  -3  5] 3  6  7       -1  
 1  3  -1 [-3  5  3] 6  7        3  
 1  3  -1  -3 [5  3  6] 7        5  
 1  3  -1  -3  5 [3  6  7]       6 

Example 2:

Input: nums = [1,2,3,4,2,3,1,4,2], k = 3
Output: [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]

Constraints


  • 1 <= k <= nums.length <= 105
  • -231 <= nums[i] <= 231 - 1

Solution


We will slide a window through all the data, to know the median we need the values in the window to be in order, then we know that the element in the middle (or the mean of the two central elements) is our median.

We can handle ourselves the list of elements in the window, sort them, and then when we move the window we have to remove the element with the lower value, and insert a new one in its corresponding sorted position… but, to do it much more simple, we can use multiset, it allows us to have multiple repeated values and it will be sort, when we insert a new element, it will be placed in the correct position.

Then we just need to look to the central elemente(s) and we know the median.

To learn more about multisets see these link:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
  vector<double> medianSlidingWindow(vector<int>& nums, int k) {
    vector<double> result;
    int mid = (k / 2);
    bool even = k % 2 == 0;

    multiset<int> w;
    int i = 0;

    for (i=0; i<k-1; i+=1)
      w.insert(nums[i]);

    while (i<nums.size()) {
      w.insert(nums[i]);
      long long sum = 0;

      auto n = w.begin();
      advance(n, mid);
      sum += (long long)*n;
        
      if (even) {
        n = w.begin();
        advance(n, mid - 1);
        sum += (long long)*n;
      }
          
      result.push_back(even ? (double)sum / (double)2.0: (double)sum);
      w.erase(w.lower_bound(nums[i-k+1]));

      i += 1;
    }

    return result;
  }
};
This post is licensed under CC BY 4.0 by the author.

Partition Labes

Sliding Window Maximum