Debounced Call Times
Problem statement
A debounced function waits for its callers to go quiet before it does any work. Each call says: run me wait time units from now, with my value. If another call comes before that moment, the waiting run is cancelled and the new call starts its own wait.
Call i happens at time times[i] with value values[i]. The rules:
- Each call cancels the run that is waiting (if any) and schedules a run at
times[i] + waitwithvalues[i]. - A waiting run that is due at time
Thappens before any call that arrives at timeTor later, so such a call cannot cancel it. - Calls at the same time are handled in the order they are given.
Return every run that actually happens as a pair [run time, value], in time order.
Function
debouncedRuns(times: int[], values: int[], wait: int) → int[][]Examples
Example 1
times = [1,3,4,10,11,20]values = [5,6,7,8,9,10]wait = 3return = [[7,7],[14,9],[23,10]]Calls at 1, 3 and 4 come less than 3 apart, so each one pushes the run back; the run happens at 4 + 3 = 7 with the last value, 7. The calls at 10 and 11 form the next burst (run at 14 with 9), and the lone call at 20 runs at 23 with 10.
Example 2
times = [0,5]values = [1,2]wait = 5return = [[5,1],[10,2]]The first call's run is due at 5. The second call arrives exactly at 5, so the run has already happened: both calls run, at 5 and at 10.
Example 3
times = [2,2,2]values = [3,1,4]wait = 1return = [[3,4]]Three calls at the same moment form one burst. Only the last one counts: one run at 3 with value 4.
Constraints
1 <= times.length <= 1000andvalues.length == times.length0 <= times[i] <= 10^6, andtimesis sorted in non-decreasing order0 <= values[i] <= 10^41 <= wait <= 10^6