Sprinkler Running Time
A garden sprinkler runs for duration seconds each time its sensor fires. A firing while the sprinkler is already running restarts the countdown from that moment, so the sprinkler keeps running and stops duration seconds after the latest firing.
fires lists the firing times in seconds, in strictly increasing order. Return the total number of seconds the sprinkler runs. An empty fires gives 0.
Example 1
fires [1,4]
duration 2
returns 4
The first firing covers seconds 1 and 2, the second covers 4 and 5: four seconds in total.
Example 2
fires [1,2]
duration 2
returns 3
The second firing comes after only one second, restarting the countdown: the sprinkler runs from second 1 through second 3.
Constraints
0 <= len(fires) <= 1000 <= fires[i] <= 1000, strictly increasing1 <= duration <= 1000
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.