C++ simple


  • 0
    class Solution {
    public:
        vector<int> maxSlidingWindow(vector<int>& nums, int k) {
            deque<int> deq;
            vector<int> ans;
            if(nums.empty()||k==0) return ans;
    
            for(int i=0;i<nums.size();i++) {
                while(!deq.empty() && nums[i]>nums[deq.back()]) deq.pop_back();
                deq.push_back(i);
                if(deq.front()==i-k) deq.pop_front();
                if(i>=k-1) ans.push_back(nums[deq.front()]);
            }
            return ans;
        }
    };
    

Log in to reply
 

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