Teemo Attacking (LeetCode 495): Solution and Explanation
Each attack poisons for a fixed duration and a new attack resets the timer. Return how many seconds the target is poisoned in total.
Intuition
Each attack opens a poison window of length duration. The answer is the length of the union of all these windows.
Because the attacks are sorted, a window can only be cut short by the next attack. When the next attack lands, the timer resets and that attack takes over.
So attack i owns exactly min(duration, timeSeries[i+1] - timeSeries[i]) seconds, and the last attack owns the full duration. Summing those gives the union without ever merging intervals.
Approach: Sum of clipped windows
- Set total = 0.
- For each adjacent pair of attacks, compute gap = timeSeries[i+1] - timeSeries[i] and add min(duration, gap).
- Add duration once more for the final attack.
- Return total.
Step-by-step example
timeSeries = [1,4], duration = 2
- total = 0.
- Attack at 1, next attack at 4: gap 3 is at least duration 2, so add 2. total = 2.
- The last attack at 4 has nothing after it, so add the full 2. total = 4.
- Return 4: seconds 1, 2, 4 and 5 are poisoned.
Code: Python, Java, C++
1class Solution:
2 def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
3 total = 0
4 for i in range(len(timeSeries) - 1):
5 gap = timeSeries[i + 1] - timeSeries[i]
6 total += min(duration, gap)
7 total += duration
8 return total1class Solution {
2 public int findPoisonedDuration(int[] timeSeries, int duration) {
3 int total = 0;
4 for (int i = 0; i + 1 < timeSeries.length; i++) {
5 int gap = timeSeries[i + 1] - timeSeries[i];
6 total += Math.min(duration, gap);
7 }
8 total += duration;
9 return total;
10 }
11}1class Solution {
2public:
3 int findPoisonedDuration(vector<int>& timeSeries, int duration) {
4 int total = 0;
5 for (size_t i = 0; i + 1 < timeSeries.size(); i++) {
6 int gap = timeSeries[i + 1] - timeSeries[i];
7 total += min(duration, gap);
8 }
9 total += duration;
10 return total;
11 }
12};Time and space complexity
O(n)
One pass over the attacks, constant work per attack.
O(1)
Only the running total and the current gap.
Other approaches
Track the poison end time (time O(n), space O(1))
Keep end = the second the poison wears off. For each attack, add duration minus whatever part of [t, t + duration) is already before end. Same result, one more variable.
Mark every second (time O(n * duration), space O(max time))
Mark each poisoned second in a set or boolean array and count them. Correct, but times and duration go up to 10^7, so it is far too slow and too large.
Common mistakes
- Adding duration for every attack and forgetting that overlapping windows must not be counted twice.
- Forgetting the last attack, which always contributes the full duration.
- Treating the window as [t, t + duration] with duration + 1 seconds. The poison covers t through t + duration - 1.
Frequently asked questions
Why does only the next attack matter?
The array is sorted, so the next attack is the earliest one that can land inside the current window. Once it lands the timer resets, and any later overlap is that attack's business.
What if two attacks happen in the same second?
The gap is 0, so the first one adds 0 seconds and the second carries the whole window. The formula handles it with no special case.
Can the total overflow a 32-bit int?
No. The poisoned seconds all lie between the first attack and the last attack plus duration, which is at most about 2 * 10^7.