Problem
Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly duration seconds. More formally, an attack at second t will mean Ashe is poisoned during the inclusive time interval [t, t + duration - 1]. If Teemo attacks again before the poison effect ends, the timer for it is reset, and the poison effect will end duration seconds after the new attack.
You are given a non-decreasing integer array timeSeries, where timeSeries[i] denotes that Teemo attacks Ashe at second timeSeries[i], and an integer duration.
Return the total number of seconds that Ashe is poisoned.
Algorithm
Calculate the sum of the sizes of each adjacent interval. If the sum exceeds the duration, use the duration as the value; otherwise, use the actual sum of the intervals.
Code
python3
class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
ans, tlen = duration, len(timeSeries)
for i in range(1, tlen):
if timeSeries[i] - timeSeries[i-1] > duration:
ans += duration
else:
ans += timeSeries[i] - timeSeries[i-1]
return ans