Online Stock Span
MediumStack
Decode it first. Clues, constraints, pattern and plan before any code.Build a class StockSpanner that receives a stock's price one day at a time and reports each day's span. The span of today's price is the number of consecutive days, counting today and going backwards, on which the price was less than or equal to today's price.
next(price)records today's price and returns today's span.
prices in order: 100, 80, 60, 70, 60, 75, 85
spans returned: 1, 1, 1, 2, 1, 4, 6For 75, the run backwards is 75, 60, 70, 60 (four days), and it stops at 80, which is bigger.
Constraints
1 <= price <= 10^5- At most
10^4calls are made tonext.