Skip to the code
cf.
+50 XP

#722 Debounce with a Maximum Wait

hard

An editor autosaves with a debounce of wait ms, but during non-stop typing it must still save at least every maxWait ms. The rules:

  • A burst starts with a call when no save is pending. Its start time is burstStart.
  • Every call in a burst (including the first) sets the pending save time to Math.min(time + wait, burstStart + maxWait).
  • The pending save happens at that time unless a later call changes it first. After a save, nothing is pending, so the next call starts a new burst.

If a call happens at exactly the time a save is due, the save happens first. Write debounceMaxWait(calls, wait, maxWait) that returns the save times in order. calls is sorted ascending.

Example 1

Input: calls = [0,100,200,300,400,500], wait = 150, maxWait = 350
Output: [350,650]

The burst starts at 0, so saves can't wait past 350. The call at 400 starts a new burst, and the last call at 500 sets the save to 650.

Example 2

Input: calls = [0,1000], wait = 100, maxWait = 500
Output: [100,1100]

Calls far apart behave like a plain debounce.

Constraints

  • 0 <= calls.length <= 10^5
  • 0 <= calls[i] <= 10^9, sorted ascending.
  • 1 <= wait <= maxWait <= 10^6
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).