C++ multiset


  • 0
    B
    class Solution {
    public:
        vector<double> medianSlidingWindow(vector<int>& nums, int k) {
            multiset<long>st;
            vector<double>ans;
            int i = 0;
            while(i + 1 < k) st.insert(nums[i++]);
            for(;i < nums.size();i++)
            {
                st.insert(nums[i]);
                auto it1 = st.begin();
                auto it2 = st.begin();
                advance(it1, (k-1)/2);
                advance(it2, k/2);
                ans.push_back(double(*it1 + *it2)/2);
                st.erase(st.find(nums[i-k+1]));
            }
            return ans;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.