[Sliding Window] leetcode 239 Sliding Window Maximum

Keywords: PHP

problem:https://leetcode.com/problems/sliding-window-maximum/

Solution 1: Using balanced binary tree (map), time complexity: O (N * log K)

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> res;
        if (nums.size() == 0) return res;
        map<int,int> count;
        for (int i = 0; i < k; i++)
        {
            count[nums[i]]++;
        }
        res.push_back(count.rbegin()->first);
        for (int i = k; i < nums.size(); i++)
        {
            // erase
            count[nums[i - k]]--;
            if (count[nums[i - k]] == 0)
            {
                count.erase(nums[i - k]);
            }

            // insert
            count[nums[i]]++;

            res.push_back(count.rbegin()->first);
        }

        return res;
    }
};

Solution 2: Maintain the subscript of maximum data and update the subscript according to the movement of sliding window. Time Complexity: Approximate O(N) under equalization

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> res;
        if (nums.size() == 0) return res;
        
        int maxnum = INT_MIN;
        int index;
        for (int i = 0; i < k; i++)
        {
            if (nums[i] > maxnum)
            {
                maxnum = nums[i];
                index = i;
            }
            maxnum = max(nums[i], maxnum);
        }
        res.push_back(maxnum);
        for (int i = k; i < nums.size(); i++)
        {
            if (nums[i] > maxnum)
            {
                maxnum = nums[i];
                index = i;
            }

            else if (i - k == index)
            {
                maxnum = INT_MIN;
                for (int j = index + 1; j <= i; j++)
                {
                    if (nums[j] > maxnum)
                    {
                        maxnum = nums[j];
                        index = j;
                    }
                }
            }

            res.push_back(maxnum);
        }

        return res;
    }
};

Solution 3: monotonous queue. Maintain a monotonically decreasing queue, because when the largest data is removed, we always want to find the second largest data. If we maintain a monotonically decreasing queue, we will remove the head of the queue, and the new head of the queue will naturally be the second largest. Time Complexity: Approximate O(N) under equalization

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

            if (i >= k - 1)
            {
                ans.push_back(nums[monoQueue.front()]);
            }
        }
        return ans;
    }
};

Posted by Eddie Fisher on Tue, 30 Jul 2019 20:00:32 -0700