Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
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 <= 1040 <= timeSeries[i], duration <= 107timeSeries is sorted in non-decreasing order.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,4] 2
[1,2] 2
merge-intervals)can-place-flowers)dota2-senate)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #495: Teemo Attacking
class Solution {
public int findPoisonedDuration(int[] timeSeries, int duration) {
int n = timeSeries.length;
int ans = duration;
for (int i = 1; i < n; ++i) {
ans += Math.min(duration, timeSeries[i] - timeSeries[i - 1]);
}
return ans;
}
}
// Accepted solution for LeetCode #495: Teemo Attacking
func findPoisonedDuration(timeSeries []int, duration int) (ans int) {
ans = duration
for i, x := range timeSeries[1:] {
ans += min(duration, x-timeSeries[i])
}
return
}
# Accepted solution for LeetCode #495: Teemo Attacking
class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
ans = duration
for a, b in pairwise(timeSeries):
ans += min(duration, b - a)
return ans
// Accepted solution for LeetCode #495: Teemo Attacking
struct Solution;
impl Solution {
fn find_poisoned_duration(time_series: Vec<i32>, duration: i32) -> i32 {
let n = time_series.len();
if n == 0 {
return 0;
}
let mut start = time_series[0];
let mut res = 0;
for i in 1..n {
let end = time_series[i];
if start + duration > end {
res += end - start;
} else {
res += duration;
}
start = end;
}
res += duration;
res
}
}
#[test]
fn test() {
let time_series = vec![1, 4];
let duration = 2;
let res = 4;
assert_eq!(Solution::find_poisoned_duration(time_series, duration), res);
let time_series = vec![1, 2];
let duration = 2;
let res = 3;
assert_eq!(Solution::find_poisoned_duration(time_series, duration), res);
}
// Accepted solution for LeetCode #495: Teemo Attacking
function findPoisonedDuration(timeSeries: number[], duration: number): number {
const n = timeSeries.length;
let ans = duration;
for (let i = 1; i < n; ++i) {
ans += Math.min(duration, timeSeries[i] - timeSeries[i - 1]);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.