Build a tool that reads the daily price of a share, one day at a time, and tells you the span of the current day.
The span of a day is the number of days in a row, going back from today and counting today, on which the price was not higher than today's price. It stops at the first earlier day with a higher price.
- Example: the last three prices were
[9, 4, 6] and today's price is 6. The span is 3. We count today, the earlier 6, and the 4. The 9 is higher, so we stop.
- Example: the last three prices were
[3, 5, 8] and today's price is 2. The span is 1, because yesterday's 8 is higher.
Write a class StockSpanner with these methods:
StockSpanner(): create the tool.
next(price): give today's price and get back today's span.
Examples
Example 1:
Input
["StockSpanner", "next", "next", "next", "next", "next", "next"]
[[], [50], [40], [45], [60], [55], [70]]
Output
[null, 1, 1, 2, 4, 1, 6]
Explanation
50 and 40 each have a span of 1.
45 is above 40 but below 50, so its span is 2.
60 is above 45, 40 and 50, so its span is 4.
55 is below 60, so its span is 1.
70 is above all of the earlier prices, so its span is 6.
Example 2:
Input
["StockSpanner", "next", "next", "next"]
[[], [30], [30], [30]]
Output
[null, 1, 2, 3]
Explanation
Equal prices count too, so the spans are 1, 2 and 3.