#495
Easy Algorithms Teemo attacking
Array Simulation
57.5% acceptance
Jan 13, 2026
1349
157
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn find_poisoned_duration(time_series: Vec<i32>, duration: i32) -> i32 {
if time_series.is_empty() {
return 0;
}
let mut total = 0;
for i in 0..time_series.len() - 1 {
total += (time_series[i + 1] - time_series[i]).min(duration);
}
total + duration
}
}