Problem
Design a class that gets the daily price of a stock, one day at a time. For each new price, the class returns the span of the stock for that day.
The span is the largest number of days in a row that end today and have a price at most equal to the price today. Today counts as one of these days.
Write the class StockSpanner with these operations:
StockSpanner() creates the object with no prices. next(price) adds price as the price for the next day. It returns the span for that day.
Example 1
- Input:
- ["StockSpanner","next","next","next","next","next"] [[],[50],[40],[45],[60],[30]]
- Output:
- [null,1,1,2,4,1]
- Explanation:
- For the price 45, the prices 45 and 40 are not larger than 45, so the span is 2. For 60, all four prices count.
Example 2
- Input:
- ["StockSpanner","next","next","next"] [[],[10],[10],[10]]
- Output:
- [null,1,2,3]
- Explanation:
- Equal prices count, so the span grows by 1 each day.
Constraints
- 1 <= price <= 10^5
- At most 10^4 calls to next
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Best Time to Buy and Sell Stock
Coding · Array · Dynamic Programming
- Best Time to Buy and Sell Stock II
Coding · Array · Dynamic Programming · Greedy
- Best Time to Buy and Sell Stock III
Coding
- Best Time to Buy and Sell Stock IV
Coding
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
Meta
Leetcode 901. Online Stock Span
Reported Feb 18, 2025 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View source