495. Teemo Attacking
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the total duration an enemy, Ashe, is poisoned. Teemo attacks at specific times given in a sorted array timeSeries. Each attack applies poison for a fixed duration. If a new attack occurs before the current poison effect wears off, the poison timer is reset. This means the new poison period starts from the new attack time and lasts for the full duration. The core challenge is to correctly calculate the total poisoned time, accounting for these overlapping poison effects.
Solution
Teemo Attacking in Python
class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
# According to constraints, timeSeries is not empty.
# If duration is 0, the calculation will correctly yield 0.
if duration == 0:
return 0
# Python integers handle arbitrary size, so no overflow concerns.
total_duration = 0
n = len(timeSeries)
# Iterate through consecutive pairs of attacks.
for i in range(n - 1):
# Calculate the actual poisoned time for the interval between
# attack i and attack i+1.
# It's the minimum of the full duration and the time until the next attack.
diff = timeSeries[i+1] - timeSeries[i]
total_duration += min(duration, diff)
# The last attack always adds a full 'duration' of poison time.
total_duration += duration
return total_duration
Teemo Attacking in Java
class Solution {
public int findPoisonedDuration(int[] timeSeries, int duration) {
// According to constraints, timeSeries length >= 1 and duration >= 0.
// If duration is 0, the result will be correctly calculated as 0.
if (duration == 0) {
return 0;
}
// Use long to prevent potential integer overflow.
// The maximum result could be 10^4 * 10^7 = 10^11, which exceeds Integer.MAX_VALUE.
long totalDuration = 0;
int n = timeSeries.length;
// Iterate through the attacks up to the second to last one.
for (int i = 0; i < n - 1; i++) {
// Calculate time difference between consecutive attacks.
int diff = timeSeries[i + 1] - timeSeries[i];
// Add the minimum of the duration and the time difference to the total.
// This accounts for overlapping poison effects.
totalDuration += Math.min(duration, diff);
}
// The last attack contributes its full duration as it's not interrupted.
totalDuration += duration;
return (int)totalDuration;
}
}
Teemo Attacking in C++
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
// According to constraints, timeSeries is not empty and duration >= 0.
// If duration is 0, the loop will correctly produce 0, and adding duration (0) gives 0.
if (duration == 0) {
return 0;
}
// Use long long to prevent potential integer overflow.
// The maximum possible result can be 10^4 * 10^7 = 10^11, which exceeds a 32-bit integer.
long long totalDuration = 0;
int n = timeSeries.size();
// Iterate through the attacks from the first to the second to last.
for (int i = 0; i < n - 1; ++i) {
// Calculate the time difference between consecutive attacks.
int diff = timeSeries[i + 1] - timeSeries[i];
// The poisoned duration for the current interval is the minimum of
// the full duration and the time until the next attack.
totalDuration += std::min(duration, diff);
}
// The last attack always adds the full duration since nothing interrupts it.
totalDuration += duration;
return totalDuration;
}
};
Teemo Attacking in TypeScript
function findPoisonedDuration(timeSeries: number[], duration: number): number {
// According to constraints, timeSeries is not empty.
// If duration is 0, the result will be correctly calculated as 0.
if (duration === 0) {
return 0;
}
// TypeScript's number is a 64-bit float, which can safely handle the sum.
let totalDuration = 0;
const n = timeSeries.length;
// Iterate through the attacks, considering pairs of consecutive attacks.
for (let i = 0; i < n - 1; i++) {
// Calculate the difference in time between the current and next attack.
const diff = timeSeries[i + 1] - timeSeries[i];
// The duration of poison from the current attack is limited by the next attack.
// So we take the minimum of the full duration and the time difference.
totalDuration += Math.min(duration, diff);
}
// The very last attack will always last for the full duration.
totalDuration += duration;
return totalDuration;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.