Standard template library for solving algorithm problems

Keywords: C++ Algorithm data structure leetcode

Monotone stack handles the problem of size comparison in the overall O(n) time by maintaining the monotonic increment (decrement) of the value in the stack.

739 daily temperature

Given the daily temperature, it is required to wait a few days for each day to wait for a warmer day. If there is no warmer weather after that day, it is recorded as 0.

The input is a one-dimensional integer array, and the output is an integer array of the same length, indicating how many days to wait for each day.

Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]

Resolution:

This problem can express the daily temperature by maintaining a monotonically decreasing stack; This question requires the difference between the current temperature and higher temperature, so the elements in the stack represent the date corresponding to the temperature, that is, the position of the temperature in the array. Monotonically decreasing stack, so the top of the stack represents the date of the lowest temperature in the stack.

Traverse the array from left to right. For date P, if the temperature corresponding to p is higher than the temperature corresponding to date q at the top of the stack, it will encounter a warmer day. Take q out of the stack and record the difference of days as p - q; If the temperature corresponding to p is lower than the temperature corresponding to the date q at the top of the stack, or the stack is empty, press P into the stack, and then continue to consider the next day.

After traversing the array, the temperature corresponding to the remaining dates in the stack decreases, indicating that there is no warmer weather after them.

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        stack<int> s;
        int len = temperatures.size();
        vector<int> ans(len);
        for(int i=0;i<len;++i){
            // The date in the stack that is lower than the temperature of the current date, and calculate the difference of days
            while(!s.empty()){
                int preTemp = temperatures[s.top()];
                // If the stack top date temperature is greater than the current date temperature, stop the stack
                if(preTemp >= temperatures[i]){
                    break;
                }
                ans[s.top()] = i - s.top();
                s.pop();
            }
            s.push(i);
        }
        // After the traversal is completed, there is no higher temperature after the date that has not been out of the stack
        while(!s.empty()){
            ans[s.top()] = 0;
            s.pop();
        }
        return ans;
    }
};

503 next larger element II

Given a circular array (the next element of the last element is the first element of the array), the next larger element of each element is output. If not, output - 1.

The input is a one-dimensional integer array, and the output is an integer array of the same length, indicating the next larger element for the number of positions of the original array.

Input: [1,2,1]
Output: [2, - 1,2]
Explanation: the next larger number of the first 1 is 2; The number 2 cannot find the next larger number; The next maximum number of the second 1 needs a circular search, and the result is also 2.

Resolution:

Similar to the daily temperature, this problem can also represent the position of the original array elements by maintaining a monotonically decreasing stack.

This topic and 739 daily temperature The difference is that the array to be traversed is A circular array. The common way to traverse the circular array is to straighten the loop, that is, copy the first n-1 elements of the sequence and splice them behind the original sequence. That is, A copy of the original array is spliced with itself to form an array. For example, if the ABCDEF circular array is straightened to ABCDEFABCDEF, there will be A to A, B to B... F to F.

More simply, it can avoid increasing space overhead. You can directly traverse the array with modular operation. For example, the circular array element with length n is expressed as array[i%n]

According to the above circular array traversal method, traverse from left to right. For the current position P, if the element value corresponding to p is greater than the element value corresponding to q at the top of the monotonic stack, the larger value encountered by q will retain the value corresponding to P at q. If the stack is empty, or the element value corresponding to p is less than the element value corresponding to q at the top of the monotonic stack, press P into the stack, and then consider the next element.

After traversing the array, the element values corresponding to the remaining positions in the stack are decreasing, indicating that there are no larger elements after them.

class Solution {
public:
    vector<int> nextGreaterElements(vector<int>& nums) {
        stack<int> s;
        int len = nums.size();
        // ans is initialized to - 1, so you don't have to consider the elements that are not out of the stack
        vector<int> ans(len,-1);
        for(int i=0;i<2*len-1;++i){
            while(!s.empty()){
                int preVal = nums[s.top()%len];
                if(preVal >= nums[i%len]){
                    break;
                }
                ans[s.top()%len] = nums[i%len];
                s.pop();
            }
            s.push(i);
        }
        return ans;
    }
};

reference material

LeetCode 101: easily brush questions with you (C + +) Chapter 11 wonderful use of data structure

Posted by r3dk1t on Thu, 21 Oct 2021 22:53:03 -0700