dsa · easy

Poison Spray Time

A champion sprays toxin at increasing times time_series[i] (seconds). Each spray poisons the target for exactly duration seconds, starting at that instant. A new spray while the target is still poisoned does not stack — it only restarts the timer from the new instant.

Arguments

Return the total number of seconds the target is poisoned.

Example

time_series = [1,4], duration = 2

Spray at t=1 covers [1, 3). Spray at t=4 covers [4, 6). The two windows do not overlap, so the total is 2 + 2 = 4.

time_series = [1,2], duration = 2

The first window is [1, 3). The second starts at 2, inside that window, and covers [2, 4). Merged coverage is [1, 4)3.

Constraints

1 <= time_series.length <= 4*10^4 0 <= time_series[i] <= 10^7 time_series is sorted in non-decreasing order All time_series[i] are unique 1 <= duration <= 10^6 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[1,4]
2

Expected:
4

Example 2

Input:
[1,2]
2

Expected:
3

Example 3

Input:
[1]
5

Expected:
5

Open in the Dojo editor