Algorithm


Problem Name: 495. Teemo Attacking

Problem Link: https://leetcode.com/problems/teemo-attacking/

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.

 

Example 1:

Input: timeSeries = [1,4], duration = 2
Output: 4
Explanation: Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.

Example 2:

Input: timeSeries = [1,2], duration = 2
Output: 3
Explanation: Teemo's attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.

 

Constraints:

  • 1 <= timeSeries.length <= 104
  • 0 <= timeSeries[i], duration <= 107
  • timeSeries is sorted in non-decreasing order.

 

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int findPoisonedDuration(int[] timeSeries, int duration) {
    int totalDuration = 0;
    int prevTime = timeSeries[0];
    for (int i = 1; i  <  timeSeries.length; i++) {
      totalDuration += Math.min(duration, timeSeries[i] - prevTime);
      prevTime = timeSeries[i];
    }
    return totalDuration + duration;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
timeSeries = [1,4], duration = 2

Output

x
+
cmd
4

#2 Code Example with Javascript Programming

Code - Javascript Programming


const findPoisonedDuration = function(timeSeries, duration) {
  if (timeSeries == null || timeSeries.length === 0) return 0
  let res = 0
  for (let i = 1, len = timeSeries.length; i  <  len; i++) {
    const tmp =
      timeSeries[i - 1] + duration > timeSeries[i]
        ? timeSeries[i] - timeSeries[i - 1]
        : duration
    res += tmp
  }
  return res + duration
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
timeSeries = [1,4], duration = 2

Output

x
+
cmd
4

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def findPoisonedDuration(self, timeSeries, duration):
        """
        :type timeSeries: List[int]
        :type duration: int
        :rtype: int
        """
        timeSeries.sort()
        res, upper = 0, 0
        for i, num in enumerate(timeSeries): 
            if num > upper: upper = num
            res, upper = res + num + duration - upper, num + duration
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
timeSeries = [1,2], duration = 2

Output

x
+
cmd
3

#4 Code Example with C# Programming

Code - C# Programming


using System;

namespace LeetCode
{
    public class _0495_TeemoAttacking
    {
        public int FindPoisonedDuration(int[] timeSeries, int duration)
        {
            int n = timeSeries.Length;
            if (n == 0) return 0;

            int total = 0;
            for (int i = 0; i  <  n - 1; ++i)
                total += Math.Min(timeSeries[i + 1] - timeSeries[i], duration);
            return total + duration;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
timeSeries = [1,2], duration = 2

Output

x
+
cmd
3
Advertisements

Demonstration


Previous
#494 Leetcode Target Sum Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#496 Leetcode Next Greater Element I Solution in C, C++, Java, JavaScript, Python, C# Leetcode