【单调栈】LeetCode 901. 股票价格跨度

题目链接

股票价格跨度

注意事项

使用单调栈

代码

class StockSpanner {
public:
    StockSpanner() {

        this->stk.emplace(-1, INT_MAX);
        this->idx = -1;
    }

    int next(int price) {

        idx++;
        while (price >= stk.top().second) {
            stk.pop();
        }

        int ret = idx - stk.top().first;
        stk.emplace(idx, price);
        
        return ret;
    }

private:
    stack<pair<int, int>> stk;
    int idx;
};
posted @ 2022-10-23 09:48  Frodo1124  阅读(18)  评论(0编辑  收藏  举报