Skip to the code
cf.
+25 XP

#266 Price Streak Tracker

medium

A crypto ticker shows a streak badge each day: how many days in a row, ending today and counting backwards, the price was less than or equal to today's price. Write a class PriceStreak:

  • new PriceStreak() starts with no days.
  • next(price) adds today's price and returns today's streak. Today always counts, so the streak is at least 1.

Each example lists the calls in order, then each call's arguments. The first call creates the object with new PriceStreak(…); the output lists what each call returns (null for the constructor and for methods that return nothing).

Example 1

Input: ["PriceStreak","next","next","next","next","next","next","next"]
[[],[100],[80],[60],[70],[60],[75],[85]]
Output: [null,1,1,1,2,1,4,6]

On the 75 day the prices 75, 60, 70, 60 are all at most 75, so the streak is 4. 85 covers six days.

Example 2

Input: ["PriceStreak","next"]
[[],[10]]
Output: [null,1]

A single day has a streak of 1.

Constraints

  • 1 <= price <= 10^5
  • At most 10^4 calls.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).