Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

93. Online Stock Span

MediumDay 27 of 60Stack & Queue

Design a StockSpanner class that receives a stock price every day through next(price) 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 the span is at least 1.

Example 1
Input: ops = ["StockSpanner","next","next","next","next","next","next","next"], args = [[],[100],[80],[60],[70],[60],[75],[85]]
Output: [null,1,1,1,2,1,4,6]
For 75, the previous prices 60, 70 and 60 are all at most 75, so the span is 4.
Constraints
  • 1 <= price <= 10^5
  • At most 10^4 calls to next
Hints
Frequently asked at: Amazon, Microsoft, Goldman Sachs
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min