901 Online Stock Span

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

solution

class StockSpanner:
    def __init__(self):
        self.stack = []  # This stack will keep track of stock prices and their spans

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

时间复杂度:O() 空间复杂度:O()

Last updated