Leetcode 239 Sliding Window Maximum Solution in c++ | Hindi Coding Community

0

  

You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return the max sliding window.


class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> d;
        deque<int> c;
        map<int,int> h;
        for(int i=0;i<k;i++){
            c.push_back(nums[i]);
            h[nums[i]]+=1;
        }
        d.push_back(h.rbegin()->first);
        while(k!=nums.size()){
            c.push_back(nums[k]);
            h[nums[k]]+=1;
            int de=c.front();
            h[de]-=1;
            if(h[de]==0)h.erase(de);
            c.pop_front();
            d.push_back(h.rbegin()->first);
            k+=1;
        }
        return d;
    }
};

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !