Code Caprice Algorithm Training Camp Day 12 | Summary of Stacks and Queues

LeetCode239. Maximum Sliding Window

topic link

https://leetcode.cn/problems/sliding-window-maximum/

topic analysis

When designing a monotonic queue, the pop and push operations should maintain the following rules:

  1. pop(value): If the value of the element removed by the window is equal to the exit element of the monotonic queue, then the queue pops the element, otherwise no operation is performed

  1. push(value): If the value of the push element is greater than the value of the entry element, then pop the element of the queue entry until the value of the push element is less than or equal to the value of the queue entry element

Keeping the above rules, every time the window moves, just ask que.front() to return the maximum value of the current window.

In order to feel the working process of the monotonic queue more intuitively, take the title example as an example, input: nums = [1,3,-1,-3,5,3,6,7], and k = 3, the animation is as follows:

code show as below:

class Solution {
private:
    class MyQueue {
    public:
        deque<int> que;//定义一个队列,实现单调队列的功能
        
        //pop操作,当要删除的值与队列出口的值相等时,将队列出口的值pop掉
        //否则,无需进行操作
        //注意,要实现确定队列不为空,以保证pop操作有效。
        void pop(int value) {
            if(!que.empty() && value == que.front()) {
                que.pop_front();
            }
        }

        //push操作,当要插入的值比队列入口的值大时,将队列入口的值弹出
        //循环进行,直到队列为空或者插入的值比队列入口的值小,此时将要插入的值插入
        //保证了队列中的元素是从大到小的顺序排列
        void push(int value) {
            while(!que.empty() && value > que.back()) {
                que.pop_back();
            }
            que.push_back(value);
        }
        
        //返回当前队列的出口元素,也就是最大值
        int refront() {
            return que.front();
        }
    };
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        MyQueue que;
        vector<int> result;
        //事先把第一组窗口数值放入
        for(int i = 0; i < k; i++) {
            que.push(nums[i]);
        }
        //返回最大值
        result.push_back(que.refront());
        //窗口滑动,先pop再push,然后返回最大值
        for(int j = k; j < nums.size(); j++) {
            que.pop(nums[j - k]);
            que.push(nums[j]);
            result.push_back(que.refront());
        }

        return result;
    }
};

LeetCode

class Solution {

private:

class MyQueue {

public:

deque<int> que;//Define a queue to realize the function of monotonic queue

//pop operation, when the value to be deleted is equal to the value of the queue exit, pop the value of the queue exit

//Otherwise, no action is required

//Note that it is necessary to ensure that the queue is not empty to ensure that the pop operation is valid.

void pop(int value) {

if(!que.empty() && value == que.front()) {

that.pop_front();

}

}

//push operation, when the value to be inserted is greater than the value of the queue entry, pop the value of the queue entry

// Loop until the queue is empty or the value inserted is smaller than the value of the queue entry, then insert the value to be inserted

//Ensure that the elements in the queue are arranged in order from large to small

void push(int value) {

while(!que.empty() && value > que.back()) {

que.pop_back();

}

que.push_back(value);

}

// Return the exit element of the current queue, which is the maximum value

int refront() {

return que.front();

}

};

public:

vector<int> maxSlidingWindow(vector<int>& nums, int k) {

MyQueue what;

vector<int> result;

//put the first set of window values ​​in advance

for(int i = 0; i < k; i++) {

que.push(nums[i]);

}

// return the maximum value

result.push_back(que.refront());

//The window slides, first pop and then push, and then return the maximum value

for(int j = k; j < nums.size(); j++) {

that.pop(nums[j - k]);

que.push(nums[j]);

result.push_back(que.refront());

}

return result;

}

};

Guess you like

Origin blog.csdn.net/wyr1849089774/article/details/128764592