Online Stock Span
Design a StockSpanner that receives the day's stock price via successive next(price) calls. For each call, return the stock's span: the number of consecutive days ending today (including today) on which the price was less than or equal to today's price.
Open official problem prompt ↗For each incoming price, report how many consecutive prior days (up to and including today) had a price at or below today's, using only past data as it streams in.
Think of stacking plates of increasing height from the back. When a tall plate arrives, it hides all the shorter-or-equal plates in front of it, so it 'inherits' their combined coverage; only a taller plate behind it can block the view.
- Input
- next(100), next(80), next(60), next(70), next(60), next(75), next(85)
- Output
- [1, 1, 1, 2, 1, 4, 6]
- Why
- When 75 arrives it covers 60, 70, and 60 plus itself for a span of 4; 85 then covers 75, 60, 70, 60 plus itself for a span of 6, stopping at 100.
1 <= price <= 10^5At most 10^4 calls to nextPrices arrive one at a time (online / streaming)