Skip to the code
+10 XP

#720 Throttled Scroll Handler

easy

A sticky header updates on scroll, but at most once every wait milliseconds. The handler runs immediately on a scroll event if at least wait ms have passed since it last ran (or if it has never run); otherwise that event is ignored.

Write throttleRuns(calls, wait) where calls holds the scroll event times, sorted ascending. Return the times at which the handler runs.

Example 1

Input: calls = [0,100,250,600], wait = 200
Output: [0,250,600]

100 is only 100 ms after the run at 0, so it is ignored; 250 is 250 ms later, so it runs.

Example 2

Input: calls = [0,16,32,48,64], wait = 32
Output: [0,32,64]

Exactly wait ms later counts as enough.

Constraints

  • 0 <= calls.length <= 2 * 10^5
  • 0 <= calls[i] <= 10^9, sorted ascending.
  • 1 <= wait <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

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