Our hero Teemo is attacking an enemy Ashe with poison attacks. When Teemo attacks Ashe, Ashe gets poisoned for exactly duration seconds. More formally, an attack at second t means Ashe is poisoned during the inclusive time interval [t, t + duration - 1]. If Teemo attacks again before the poison effect ends, the timer is reset, and the poison effect ends duration seconds after the new attack.
You are given a non-decreasing integer array time_series, where time_series[i] means Teemo attacks Ashe at second time_series[i], and an integer duration.
Return the total number of seconds that Ashe is poisoned.
Function Signature
def find_poisoned_duration(time_series: list[int], duration: int) -> int:
Rules
-
Each second is counted once, even if several attacks cover it.
-
Two attacks can happen at the same second. The second one simply restarts the timer from that same second.
Constraints
-
1 <= len(time_series) <= 10^4
-
0 <= time_series[i], duration <= 10^7
-
time_series
is sorted in non-decreasing order.
Examples
Input: time_series = [1,4], duration = 2
Output: 4
The attack at second 1 poisons Ashe during [1, 2], and the attack at second 4 poisons her during [4, 5]. The intervals do not overlap, so the total is 4 seconds.
Input: time_series = [1,2], duration = 2
Output: 3
The attack at second 1 poisons Ashe during [1, 2]. The attack at second 2 resets the timer and extends the poisoning to [2, 3], so she is poisoned during seconds 1, 2 and 3.
Input: time_series = [1,1,3,10], duration = 3
Output: 8
The two attacks at second 1 and the attack at second 3 together cover [1, 5], which is 5 seconds. The attack at second 10 covers [10, 12], 3 more seconds, for a total of 8.