[sword finger Offer] stack and queue

Keywords: Algorithm data structure queue

Sword finger Offer 59 - I. maximum value of sliding window

Given an array num and the size k of the sliding window, please find the maximum value in all sliding windows.

Example:

input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
 output: [3,3,5,5,6,7] 
explain: 

  Position of sliding window                Maximum
---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7

Tips:

You can assume that k is always valid. When the input array is not empty, 1 ≤ k ≤ the size of the input array.

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        queue<int> q;
        deque<int> d;
        int sum = 0, n = nums.size(), maxn = 0, loc = 0;
        if(n == 0 || k == 0)return {};
        for(int i = 0 ; i < k; ++ i){
            q.push(nums[i]);
            while(d.empty() == false && d.back() < nums[i]){
                d.pop_back();
            }
            d.push_back(nums[i]);
        }
        vector<int> ans{d.front()};
        for(int i = 0; i + k < n; ++ i){
            if(d.front() == q.front()){
                d.pop_front();
            }
            q.pop();
            q.push(nums[i + k]);
            while(d.empty() == false && d.back() < nums[i + k]){
                d.pop_back();
            }
            d.push_back(nums[i + k]);
            ans.push_back(d.front());
        }
        return ans;
    }
};

Sword finger Offer 59 - II. Maximum value of queue

Please define a queue and implement the function max_value gets the maximum value in the queue and requires the function max_value,push_back and pop_ The average sharing time complexity of front is O(1).

If the queue is empty, pop_front and max_value needs to return - 1

Example 1:

input: 
["MaxQueue","push_back","push_back","max_value","pop_front","max_value"]
[[],[1],[2],[],[],[]]
output: [null,null,null,2,1,2]

Example 2:

input: 
["MaxQueue","pop_front","max_value"]
[[],[],[]]
output: [null,-1,-1]

Limitations:

  • 1 <= push_ back,pop_ front,max_ Total operands of value < = 10000
  • 1 <= value <= 10^5
class MaxQueue {
public:
    queue<int> q;
    deque<int> maxn;
    MaxQueue() {
    }
    
    int max_value() {
        if(q.empty())return -1;
        return maxn.front(); 
    }
    
    void push_back(int value) {
        while(maxn.empty() == false && maxn.back() < value){
            maxn.pop_back();
        }
        maxn.push_back(value);
        q.push(value);
    }
    
    int pop_front() {
        if(q.empty())return -1;
        int ret = q.front();
        q.pop();
        if(ret == maxn.front()){
            maxn.pop_front();
        }
        return ret;
    }
};

/**
 * 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();
 */

Algorithm idea

Today's two questions are similar, and the solutions are written together. The sliding window of question 1 is actually a queue (FIFO). Like question 2, the core of the problem is how to maintain the maximum value of the queue. Here, it is maintained by a double ended queue. When we pop up an element from the queue head, if the value is not the maximum value of the current queue, its pop-up has no impact on the maximum value, Therefore, every time an element is inserted into the end of the queue, the element is also inserted into the end of the double ended queue, but before inserting the end of the double ended queue, all elements in the end of the double ended queue that are smaller than the inserted element are ejected in advance, because they can no longer become the maximum value of the queue. When the queue header pops up the element, if it is the largest element of the queue (double ended queue header), the double ended queue also pops up the element and updates the maximum value. It is very easy to maintain interval extremum with queue and double ended queue, which is very common in solving problems such as dynamic programming

[sword finger Offer] series:
[sword finger Offer] stack
[sword finger Offer] linked list
[sword finger Offer] string
[sword finger Offer] search algorithm
[sword finger Offer] search algorithm (1)
[sword finger Offer] search and backtracking algorithm
[sword finger Offer] search and backtracking algorithm (1)
[sword finger Offer] dynamic programming
[sword finger Offer] dynamic programming (1)
[sword finger Offer] dynamic programming (2)
[sword finger Offer] double pointer
[sword finger Offer] double pointer (1)
[sword finger Offer] double pointer (2)
[sword finger Offer] search and backtracking algorithm (2)
[sword finger Offer] prime search and backtracking algorithm (3)
[sword finger Offer] sort
[sword finger Offer] sort (1)
[sword finger Offer] search and backtracking algorithm (4)
[sword finger Offer] search and backtracking algorithm (5)
[sword finger Offer] divide and conquer algorithm
[sword finger Offer] bit operation
[sword finger Offer] bit operation (1)
[sword finger Offer] mathematics
[sword finger Offer] mathematics (1)
[sword finger Offer] simulation
[sword finger Offer] string (1)

Posted by marsupillami on Wed, 06 Oct 2021 19:55:20 -0700