Description

Our hero Teemo is attacking an enemy Ashe. Teemo's attacks apply poison that lasts for a given duration. Given an array timeSeries (sorted in ascending order) representing attack times and an integer duration representing the poison duration per attack, return the total time that Ashe is poisoned. If Teemo attacks while the poison is still active, the poison timer resets to the full duration (poison effects do not stack, they refresh).

Examples

Input:timeSeries = [1,4], duration = 2
Output:4
Explanation:

Poisoned at [1,2] and [4,5] = 4 seconds.

Input:timeSeries = [1,2,3,4], duration = 3
Output:6
Explanation:

Attack at time 1 poisons until time 4, but new attacks at times 2, 3, and 4 refresh the poison. The poison expires at time 4+3=7, so total poisoned time is from 1 to 7 = 6 seconds.

Input:timeSeries = [5], duration = 10
Output:10
Explanation:

Single attack at time 5 with poison duration 10. Enemy is poisoned from time 5 to time 15, giving a total of 10 seconds of poison.

Constraints

  • 1 ≤ timeSeries.length ≤ 10^4
  • 0 ≤ timeSeries[i], duration ≤ 10^7

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!