495. Teemo Attacking LeetCode Solution

In this guide, you will get 495. Teemo Attacking LeetCode Solution with the best time and space complexity. The solution to Teemo Attacking problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Teemo Attacking solution in C++
  4. Teemo Attacking solution in Java
  5. Teemo Attacking solution in Python
  6. Additional Resources
495. Teemo Attacking LeetCode Solution image

Problem Statement of 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.

See also  45. Jump Game II LeetCode Solution

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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

495. Teemo Attacking LeetCode Solution in C++

class Solution {
 public:
  int findPoisonedDuration(vector<int>& timeSeries, int duration) {
    if (duration == 0)
      return 0;

    int ans = 0;

    for (int i = 0; i + 1 < timeSeries.size(); ++i)
      ans += min(timeSeries[i + 1] - timeSeries[i], duration);

    return ans + duration;
  }
};
/* code provided by PROGIEZ */

495. Teemo Attacking LeetCode Solution in Java

class Solution {
  public int findPoisonedDuration(int[] timeSeries, int duration) {
    if (duration == 0)
      return 0;

    int ans = 0;

    for (int i = 0; i + 1 < timeSeries.length; ++i)
      ans += Math.min(timeSeries[i + 1] - timeSeries[i], duration);

    return ans + duration;
  }
}
// code provided by PROGIEZ

495. Teemo Attacking LeetCode Solution in Python

class Solution:
  def findPoisonedDuration(self, timeSeries: list[int], duration: int) -> int:
    if duration == 0:
      return 0

    ans = 0

    for i in range(0, len(timeSeries) - 1):
      ans += min(timeSeries[i + 1] - timeSeries[i], duration)

    return ans + duration
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.