Created
January 22, 2019 01:34
-
-
Save kanrourou/9f7020625c68339ab8380fd5d19d0b98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<double> medianSlidingWindow(vector<int>& nums, int k) { | |
int len = nums.size(); | |
multiset<int> set(nums.begin(), nums.begin() + k); | |
auto mid = next(set.begin(), k / 2); | |
vector<double> res; | |
for (int i = k; i <= len; ++i) | |
{ | |
res.push_back((static_cast<double>(*mid) + static_cast<double>(*prev(mid, 1 - k % 2))) / 2.0); | |
if (i == len)break; | |
set.insert(nums[i]); | |
if (nums[i] < *mid)--mid; | |
if (nums[i - k] <= *mid)++mid; | |
set.erase(set.lower_bound(nums[i - k])); | |
} | |
return res; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment