Teemo Attacking

easy array simulation intervals

Problem

Teemo attacks at times in sorted array timeSeries; each attack poisons for duration seconds. Compute the total poisoned time, where overlapping attacks do not double-count.

InputtimeSeries = [1,4], duration = 2
Output4
Each attack adds min(duration, next − cur); last attack contributes the full duration.

def find_poisoned_duration(t, duration):
    total = 0
    for i in range(len(t) - 1):
        total += min(duration, t[i+1] - t[i])
    return total + duration if t else 0
function findPoisonedDuration(t, duration) {
  let total = 0;
  for (let i = 0; i < t.length - 1; i++) total += Math.min(duration, t[i+1] - t[i]);
  return t.length ? total + duration : 0;
}
class Solution {
    public int findPoisonedDuration(int[] t, int duration) {
        int total = 0;
        for (int i = 0; i < t.length - 1; i++) total += Math.min(duration, t[i+1] - t[i]);
        return t.length == 0 ? 0 : total + duration;
    }
}
int findPoisonedDuration(vector<int>& t, int duration) {
    int total = 0;
    for (int i = 0; i + 1 < (int)t.size(); i++) total += min(duration, t[i+1] - t[i]);
    return t.empty() ? 0 : total + duration;
}
Time: O(n) Space: O(1)