901. Online Stock Span

https://leetcode.com/problems/online-stock-span/

这个栈是个单调递减栈,如果当前的price大于等于之前的price那么之前的连续count就会加入现在的count

class StockSpanner:

    def __init__(self):
        self.stack = []
        

    def next(self, price: int) -> int:
        currCnt = 1
        while self.stack and self.stack[-1][1] <= price:
            prevCnt, _ = self.stack.pop()
            currCnt += prevCnt
        self.stack.append((currCnt,price))
        return currCnt

Last updated

Was this helpful?