class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
intervals = []
for time in timeSeries:
intervals.append([time, time+duration-1])
stack = [intervals[0]]
for i in range(1, len(intervals)):
prevInterval = stack.pop()
newInterval = intervals[i]
if prevInterval[1] >= newInterval[0]:
newInterval = [min(prevInterval[0], newInterval[0]), max(prevInterval[1], newInterval[1])]
else:
stack.append(prevInterval)
stack.append(newInterval)
ans = 0
for time in stack:
ans += (time[1] - time[0] + 1)
return ans