Teemo Attacking

Teemo attacks Ashe with poison. An attack at second `t` poisons Ashe for exactly `duration` seconds, covering the inclusive interval `[t, t + duration - 1]`. If Teemo attacks again before the poison wears off, the timer **resets**: the poison now ends `duration` seconds after the new attack. You are given a non-decreasing integer array `timeSeries`, where `timeSeries[i]` is the second of the `i`-th attack, and an integer `duration`. Return *the **total** number of seconds that Ashe is poisoned*.

30:00

Teemo Attacking
easy
Free written solution
Topics
Companies

Teemo attacks Ashe with poison. An attack at second t poisons Ashe for exactly duration seconds, covering the inclusive interval [t, t + duration - 1]. If Teemo attacks again before the poison wears off, the timer resets: the poison now ends duration seconds after the new attack.

You are given a non-decreasing integer array timeSeries, where timeSeries[i] is the second of the i-th attack, and an integer duration.

Return the total number of seconds that Ashe is poisoned.

Example 1:
Input: {"timeSeries":[1,4],"duration":2}
Output: 4
Constraints:
  • 1≤timeSeries.length≤1041 \leq \text{timeSeries.length} \leq 10^4

  • 0≤timeSeries[i],duration≤1070 \leq \text{timeSeries}[i], \text{duration} \leq 10^7

  • timeSeries\text{timeSeries} is sorted in non-decreasing order.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"timeSeries":[1,4],"duration":2}

2 attacks, each poisoning for 2s. Every attack is worth duration seconds unless the next attack resets the timer first.

poison window of each attack

t=1
t=4
1time6

poisoned total

0

this step

total = 0

seconds counted
cut off by the next attack
current attack / next attack
Variables
VariableValue
duration2
total0
DepthFunction Call
Stack empty
0/4