Total Time Covered
Description
You are given a sorted array of integers timeSeries and an integer duration. Each value in timeSeries represents the start time of an effect that lasts exactly duration seconds. If two effects overlap, they merge (you don't count the overlapping time twice). Return the total number of seconds covered by all effects combined. Simple rule: Each effect at time t covers the range [t, t + duration). If the next effect starts before the current one ends, the overlapping time is only counted once. Your function will receive solve(timeSeries, duration) and should return the total time covered.
Examples
timeSeries = [1,4], duration = 24Effect at time 1 covers [1, 3). Effect at time 4 covers [4, 6). No overlap, so total = 2 + 2 = 4 seconds.
timeSeries = [1,2,3,4], duration = 36Effects start at times 1, 2, 3, and 4, each lasting 3 seconds. They all overlap and merge into one continuous range from time 1 to time 7. Total = 7 - 1 = 6 seconds.
timeSeries = [5], duration = 1010Only one effect, starting at time 5 and lasting 10 seconds. Total = 10.
Constraints
- •
1 ≤ timeSeries.length ≤ 10^4 - •
0 ≤ timeSeries[i], duration ≤ 10^7
Ready to solve this problem?
Practice solo and sharpen your skills for technical interviews.