The sword refers to the maximum value of the sliding window of Offer 59-I/59-II/the maximum value of the queue C++

59-I

Title description

Insert picture description here

Solution deque

Maintain an ever-increasing double-ended queue from back to front. The head of the queue is the maximum value of the sliding window. When the window moves by one position, there are the following two actions.

  1. To enter a new element , starting from the end of the team, continue to pop the element smaller than it, until you find an element larger than it, and then push it into the end of the team.
    It is not difficult to understand that the newly entered number is greater than the number in the team, indicating that the number in the team is absolutely meaningless and cannot be the maximum value, and the time of going out must be earlier than the new number .
  2. To pop up the old element , you only need to determine whether the element at the head of the line is the same as the element that was popped up, and if the same is the same, it will pop up.
    You may ask, after the leader of the team is popped up, the new element is not the largest element of the mobile window. How do you know that the largest element of the mobile window is still at the leader of the team? We assume that the largest element is at the head of the team at this time, and an element smaller than it is entered later. If this element has the potential to be the maximum value of the next window, it will definitely be stored next to the head of the team. At this time, the team It's the biggest element in the first team .
    In summary, when encountering new numbers, the previous small numbers are simply discarded, and the new numbers are put in first.
class Solution {
    
    
public:
    void popNum(deque<int>& que, int value) {
    
    
        if(!que.empty() && que.front() == value) que.pop_front();
    }
    void pushNum(deque<int>& que,int value) {
    
    
        while(!que.empty() && que.back() < value) que.pop_back();
        que.push_back(value);
    }
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
    
    
        if(nums.size()==0) return {
    
    };
        deque <int> que; 
        vector<int> ans;
        //先将处理前k个元素
        for(int i = 0; i < k; i ++) {
    
    
            pushNum(que, nums[i]);
        }
        //前k个元素的最大值
        ans.push_back(que.front());
        for(int i = k; i < nums.size(); i++) {
    
    
            pushNum(que, nums[i]);
            popNum(que, nums[i - k]);
            ans.push_back(que.front());
        }
        return ans;

    }
};


Time complexity O(N), each element can be pop pushed at most once
Space complexity O(K)

59-II

Title description

Insert picture description here

Solution-assisted deque

Follow the idea just now, just create an auxiliary deque that maintains the maximum value, and then create a general queue for pop and push. When you pop, determine if the head of the deque is popped.

class MaxQueue {
    
    
public:
    //获取最大值用的
    deque<int> que;
    //存储数字
    queue<int> q;
    MaxQueue() {
    
    
    }
    
    int max_value() {
    
    
        if(que.empty()) return -1;
        return this -> que.front();
    }
    
    void push_back(int value) {
    
    
        while(!this -> que.empty() && value > this -> que.back()) this -> que.pop_back();
        this -> que.push_back(value);
        this -> q.push(value);
    }
    
    int pop_front() {
    
    
        if(q.empty()) return -1;
        if(q.front() == que.front()) que.pop_front();
        int ans = q.front();
        q.pop();
        return ans;
    }
};

/**
 * Your MaxQueue object will be instantiated and called as such:
 * MaxQueue* obj = new MaxQueue();
 * int param_1 = obj->max_value();
 * obj->push_back(value);
 * int param_3 = obj->pop_front();
 */

Insert picture description here

Time complexity O(1)
Space complexity O(N)

Guess you like

Origin blog.csdn.net/qq_42883222/article/details/112646094