Practice Online Stock Span
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
StockSpanner starts with no quotes. next(price) records today's price and returns the length of the consecutive suffix ending today in which every price is at most today's. Today always contributes one day, and an earlier higher price stops the span. Practice approach: Maintain a decreasing stack of (price, span) pairs. Pop entries whose prices are at most the new price and add their spans to one, then push the new pair. Equal prices must merge. Each quote is pushed and popped at most once, giving amortized O(1) time per call and O(n) worst-case storage.
Source: Online Stock Span on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
["StockSpanner","next","next","next","next","next"] [[],[10],[8],[8],[9],[12]]
[null,1,1,2,3,5]
The repeated price 8 spans two days. Price 9 covers the two 8s, and 12 covers every quote.
Constraints
Related LeetCode Problems
Practice more problems similar to Online Stock Span.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.