Sharing my 156ms C++ solution


  • 0
    T
    class Solution {
    public:
        vector<int> maxSlidingWindow(vector<int>& nums, int k) {
            int i, n = nums.size();
            vector<int> result;
            multiset<int> mySet;
            
            for(i=0; i<n; i++)
            {
                if(i>=k)
                    mySet.erase(mySet.find(nums[i-k]));
                mySet.insert(nums[i]);
                if(mySet.size()>=k)
                    result.push_back(*mySet.rbegin());
            }
            
            return result;
        }
    };

Log in to reply
 

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