Door chime cooldown
EasyOnline Assessment
Work out which taps on a shop's door button actually ring the chime.
The button records each tap as the second at which it happened. After a tap rings the chime, the chime is "cooling down": a tap that arrives less than cooldown seconds after the most recent tap that actually rang is ignored. A tap exactly cooldown seconds after the last ring does ring. Ignored taps do not restart the cooldown, only taps that rang do. The very first tap always rings. Taps that share the same second as a ring are ignored unless cooldown is 0.
Input: taps, a list of integers sorted in non-decreasing order, and cooldown, a non-negative integer.
Output: a list of integers, the seconds at which the chime rang, in order. If there are no taps, return [].
Example 1
- Input:
- [[0,2,5,6,9,12],5]
- Output:
- [0,5,12]
- Explanation
- The tap at 0 rings. The tap at 2 is only 2 s later, so it is ignored. The tap at 5 is exactly 5 s after the ring at 0, so it rings. The taps at 6 (1 s after the ring at 5) and 9 (4 s after) are ignored, and 12 is 7 s after the ring at 5, so it rings:
[0, 5, 12].
Example 2
- Input:
- [[2,3,3,7],4]
- Output:
- [2,7]
- Explanation
- The first tap always rings. Both taps at 3 arrive 1 s after the ring at 2 and are ignored; 7 is 5 s after the ring at 2, which is at least 4, so it rings.
Example 3
- Input:
- [[4,5,6],0]
- Output:
- [4,5,6]
- Explanation
- With a cooldown of 0 nothing is ever too soon, so every tap rings, even taps that share a second.
Constraints
0 <= taps.length <= 5,0000 <= taps[i] <= 1,000,000, andtapsis sorted in non-decreasing order (several taps can share a second)0 <= cooldown <= 1,000,000