File: teemo-attacking/solution.py

Date: 2026-06-06

Time: 19:26

teemo-attacking/solution.py

Purpose

This file solves LeetCode 495 — Teemo Attacking. It calculates the total duration that a target (Ashe) is poisoned given a series of attack timestamps and a fixed poison duration per attack. The key insight is that overlapping poison windows don't stack — a new attack resets the poison timer rather than extending it additively.

Key Components

findpoisonedduration(timeSeries, duration) -> int

The sole public function. Contract:

Patterns

Greedy adjacent-pair comparison. Rather than simulating time or merging intervals, the solution compares each consecutive pair of attacks and takes the minimum of the gap between them and the full duration. This is the standard O(n) greedy approach for interval-merge-style problems where intervals have uniform length.

The final total += duration on line 21 accounts for the last attack, which always contributes its full duration since no subsequent attack can truncate it.

Dependencies

Imports: None — pure stdlib, no external dependencies.

Imported by: teemo-attacking/test_solution.py directly. The massive "Imported By" list in the prompt is an artifact of the repo's test harness structure — those other test files don't actually import this solution; they follow the same pattern but for their own problems.

Flow

1. Guard clause: return 0 if inputs are trivially empty/zero.

2. Iterate pairs (timeSeries[i], timeSeries[i+1]) for i in [0, n-2].

3. For each pair, add min(gap, duration) to the running total. The min encodes the overlap logic: if the next attack comes before the current poison expires (gap < duration), only the gap counts; otherwise the full duration counts.

4. Add duration once for the final attack.

5. Return total.

Invariants

Error Handling

Minimal. The function guards against empty input and zero duration but does not validate types, negative values, or unsorted input. This is typical for LeetCode solutions where inputs are guaranteed by the problem constraints.

Topics to Explore

Beliefs