> For the complete documentation index, see [llms.txt](https://longxingtan.gitbook.io/mle-interview/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://longxingtan.gitbook.io/mle-interview/01_leetcode/dan-diao-zhan/901.-online-stock-span.md).

# 901. Online Stock Span

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

## solution

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