My AC 7 lines solution in C++ slow but easy to read


  • 0
    W
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        if(nums.empty()) return {};
        vector<int> res;
        int n=nums.size()-k+1;
        if(n>0){
            for(int i=0;i<n;i++) res.push_back(*max_element(nums.begin()+i,nums.begin()+i+k));
        }
        return res;
    }
    
    • step1: calculate the size of the result-> nums.size()-k+1;
    • step2: select the max value in each k subarray of nums;

Log in to reply
 

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