Skip to content

Latest commit

 

History

History
20 lines (16 loc) · 572 Bytes

901. Online Stock Span.md

File metadata and controls

20 lines (16 loc) · 572 Bytes

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()