Design a data structure that processes stock prices one by one and returns the span for each new price.
You are given a stream of daily stock prices. For each new price, compute its span: the number of consecutive days ending on the current day for which the price was less than or equal to the current price.
Implement a data structure that supports repeatedly inserting the next stock price and returning its span immediately.
The task is interactive in spirit:
next(price) is called multiple times, once per incoming stock price.price.For each call to next(price), return one integer equal to the stock span of the given price.
Example 1
Input
[
Output
[
Explanation
Illustrative example:
Calls:
Spans count consecutive days ending today with price <= current price.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.