This problem is an interval-merging question in disguise. Spotting that quickly, and then refusing to actually build the intervals, is what makes the answer clean.
Attacks happen at sorted times timeSeries[i]. Each attack poisons the target for duration seconds, and a new attack resets the timer instead of stacking. Return the total poisoned time.
Each attack covers [t, t + duration). Because the times are sorted, an attack only overlaps the one right before it. So each gap contributes min(duration, next - current).
def find_poisoned_duration(time_series, duration):
if not time_series:
return 0
total = 0
for a, b in zip(time_series, time_series[1:]):
total += min(duration, b - a)
return total + duration
The last attack always contributes the full duration, since nothing cuts it short. Time is O(n), space O(1).
duration = 0, which should return 0, and a single attack, which returns duration.If the input were unsorted or durations differed per attack, sort by start time and run the general merge-intervals sweep, tracking the current end.
Disclosure: I build interview tools at Linkjob AI. If you want live hints while practicing problems like this, try Linkjob AI's interview copilot.