- Don’t use
initand increment it by
1every time, it’s too slow.
- Use one for loop and calculate the time period by comparing if
timeSeries[i] + duration - 1 < timeSeries[i + 1].
- Also, when
i == len - 1, it’s a special case. It doesn’t meet the above condition but we still do
res += duration.
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[i] denotes that Teemo attacks Ashe at second timeSeries[i], and an integer
the total number of seconds that Ashe is poisoned.
Input: timeSeries = [1,4], duration = 2