Online Stock Span
Online Stock Span is LeetCode 901 (Medium). Design a class StockSpanner whose method next(price) receives one daily stock price at a time and returns that day's span: the number of consecutive days, ending today and going backwards, on which the price was less than or equal to today's price.
- Today always counts, so every span is at least 1.
- The prices arrive one by one. You cannot see future prices, and you must answer each call before the next one arrives.
- Up to 10⁴ calls, so walking back day by day on every call can reach about 5 × 10⁷ steps on a rising market.
- 1 <= price <= 10⁵
- At most 10⁴ calls will be made to next.
Intuition
Look at a day that was beaten: some later day had a price at least as high. Any future day that reaches back past that later day also covers the beaten day, and any future day that stops earlier never sees it. So a beaten day never needs to be checked on its own again; it can be folded into the day that beat it.
Keep a stack of (price, span) blocks, where span is how many days that block stands for. A new price pops every block whose price is not higher and adds their spans to its own. What remains on the stack has strictly falling prices from bottom to top, and each block remembers how many days it swallowed.
"How far back does the current value dominate?" asked for every element of a stream is a previous-greater-element question. When the input arrives one value at a time, the stack lives inside the class and is updated per call.
Approach
Before reading on, decide whether a day that was beaten by a later, higher price can ever end a future span. Use the answer to choose what to keep between calls. Aim for O(1) amortised per call.
Keep a stack between calls
The constructor creates an empty stack. Each entry is a pair: a price and the number of consecutive days that price block covers. The stack must survive from one next call to the next, so it is a field of the object.
Start the span at 1
Inside next(price), set span = 1 for today. Today's own price is always less than or equal to itself, so today is always part of its span.
Swallow blocks that are not higher
While the top block's price is less than or equal to price, pop it and add its span to span. Those days all sit right before today with no higher price in between, so they all belong to today's run.
Push today as one block
Push (price, span) and return span. The popped days are now stored inside today's block, so a later, higher price can absorb all of them with a single pop.
Code in Python, C++ and Java
Common pitfalls
Popping only strictly lower prices
while self.stack and self.stack[-1][0] < price:
while self.stack and self.stack[-1][0] <= price:
Equal prices are part of the span. For the calls 5, 5, the strict version leaves the first 5 on the stack and answers 1 for the second day, but the correct span is 2.
Adding 1 per pop instead of the block's span
self.stack.pop() span += 1
span += self.stack.pop()[1]
A popped block may stand for many days. For the calls 1, 2, 3, the day with price 3 pops a single block that already covers 2 days, so it must answer 3, not 2.
Edge cases
Nothing is ever popped, every span is 1, and the stack grows by one block per call. This is the case where memory reaches its maximum.
Complexity
Online Stock Span FAQ
Can I store indices instead of spans?
Yes. Count the calls in a field, push (price, index), and after popping answer index − index of the new top, or index + 1 if the stack is empty. It is the same stack; the span is just recovered from positions.
How is this related to finding the previous greater element?
Today's span ends right after the most recent day with a strictly higher price. The block left on top after the pops is exactly that day, so this class answers the previous greater element for a stream. The array version runs in Next Greater Element with the direction reversed.
Why not keep a list of all prices and walk back?
On a rising market, call k walks back k days, so 10⁴ calls cost about 5 × 10⁷ comparisons. The stack touches each day at most twice in total.