Teemo Attacking - Problem
Teemo's Poison Attack Challenge

In the world of League of Legends, our swift scout Teemo is unleashing poison attacks on the enemy champion Ashe! Each poison attack has a devastating effect that lasts for a specific duration.

How Poison Works:
• When Teemo attacks at second t, Ashe becomes poisoned for the time interval [t, t + duration - 1]
• If Teemo attacks again before the poison wears off, the poison timer resets to the new attack time
• Only one poison effect can be active at a time

Your Mission:
Given an array timeSeries representing when Teemo attacks (in chronological order) and the duration of each poison effect, calculate the total seconds that Ashe remains poisoned.

Example: If attacks happen at [1, 4] with duration 2:
• Attack 1: Poison active from second 1-2
• Attack 4: Poison active from second 4-5
• Total poisoned time: 4 seconds

Input & Output

example_1.py — Basic Case
$ Input: timeSeries = [1,4], duration = 2
Output: 4
💡 Note: Attack at time 1: poison active from 1-2 (2 seconds). Attack at time 4: poison active from 4-5 (2 seconds). Total: 4 seconds. The poison effects don't overlap since there's a gap between times 2 and 4.
example_2.py — Overlapping Attacks
$ Input: timeSeries = [1,2], duration = 2
Output: 3
💡 Note: Attack at time 1: poison would last 1-2, but attack at time 2 resets the timer. So poison is active from 1-3 continuously (3 seconds total). The second attack extends the poison duration rather than adding separate durations.
example_3.py — Single Attack
$ Input: timeSeries = [5], duration = 3
Output: 3
💡 Note: Only one attack at time 5, so poison lasts for the full duration from time 5-7, which is exactly 3 seconds.

Constraints

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

Visualization

Tap to expand
Teemo's Poison Attack VisualizationTimeAttack 1Attack 4Poison EffectPoison EffectGap = 3 secondsAlgorithm Steps:1. Attack at t=1: Next attack at t=4 Gap = 4-1 = 3, Duration = 2 ✓ Add min(3,2) = 2 seconds2. Attack at t=4: Last attack ✓ Add full duration = 2 secondsTotal Poison Time: 2 + 2 = 4 seconds🎯 Key Insight: Compare gaps between attacks vs poison duration
Understanding the Visualization
1
First Attack
Start poison timer counting down from full duration
2
Next Attack Check
If next attack comes before timer reaches 0, reset timer; otherwise count full duration
3
Sum Total Time
Add up all the time poison was actually active
Key Takeaway
🎯 Key Insight: Instead of simulating every second, we calculate poison duration by comparing the time gap between consecutive attacks with the poison duration. This reduces complexity from O(n×m) to O(n).
Asked in
Amazon 15 Google 12 Microsoft 8 Apple 5
23.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen