[Likou] Monotone stack: 901. Stock price span

[Likou] Monotone stack: 901. Stock price span

1. Introduction to the topic

Design an algorithm that collects daily quotes for some stocks and returns the span of that stock's prices for that day.

  • The span of the current day's stock price is defined as the maximum number of consecutive days (counting back from and including today) that the stock's price is less than or equal to today's price.
  • For example, if the stock price for the next 7 days is [100,80,60,70,60,75,85], then the stock span will be [1,1,1,2,1,4,6].

Code to implement StockSpanner class:

  • StockSpanner() initializes the class object.

  • int next(int price) gives today's stock price price and returns the span of the stock's price on that day.

  • Example:
    Insert image description here

2. Ideas

When calling next,

  • The input is the stock price for the new day,
  • It is necessary to return, including this day, the maximum number of consecutive days in which the stock price is less than or equal to today's stock price.

If you treat the daily price as the values ​​of different subscripts in the array, you need to find the difference between the subscripts of each value and the previous larger element. This kind of problem can be solved using a monotonic stack. The specific principle is as follows:

  • A monotonic stack is a data structure similar to a monotonic queue.
    • Monotone queue is mainly used to solve the sliding window problem.
    • The monotonic stack is mainly used to solve the NGE problem (Next Greater Element), that is, for each element in the sequence, find the next element that is larger than it. (Of course, "next" can be replaced by "previous", and "bigger than it" can also be replaced by "smaller than him". The principle remains the same.)
  • This is a bit simpler than a monotonic queue.
    • We maintain a stack representing the "elements to be determined NGE", and then traverse the sequence.
    • When we encounter a new element, we know that the element closer to the top of the stack is closer to the position of the new element.
    • Therefore, the new element is constantly compared with the top of the stack. If the new element is larger than the top of the stack, it can be concluded that the new element is the NGE of the top of the stack, so the top of the stack is popped and the comparison continues. Until the new element is no larger than the top of the stack, push the new element onto the stack. Obviously, the stack formed in this way is monotonically decreasing.

The specific solution to this problem:
The elements of the stack can be the subscript of the stock price (i.e., the number of days) and the binary number pair of the stock price, and first insert a binary number pair with the number of days as 0 days in the stack, and the maximum value as the price. , to ensure that the stack will not be empty. When calling next, all the elements in the stack whose price is less than or equal to the current price are popped out until a value greater than price is encountered, and the price is pushed into the stack, and the substandard difference is calculated and returned.

3. Problem solving code

class StockSpanner {
    
    
private:
    stack<pair<int, int>> stackp; 
    int ts;
public:
    StockSpanner() {
    
    
        this->stackp.emplace(0, INT_MAX);
        this->ts = 0;
    }
    int next(int price) {
    
    
        ts++;
        while (price >= stackp.top().second) {
    
    
            stackp.pop();	// 出栈
        }
        int res = ts - stackp.top().first;
        stackp.emplace(ts, price);	// 入栈
        return res;
    }
};

reference

【1】https://leetcode.cn/problems/online-stock-span/

Guess you like

Origin blog.csdn.net/qq_51392112/article/details/133657836