Skip to main content

Total Time Covered

EasyArrayMathSortingStackBinary Search

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

Input:timeSeries = [1,4], duration = 2
Output:4
Explanation:

Effect at time 1 covers [1, 3). Effect at time 4 covers [4, 6). No overlap, so total = 2 + 2 = 4 seconds.

Input:timeSeries = [1,2,3,4], duration = 3
Output:6
Explanation:

Effects 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.

Input:timeSeries = [5], duration = 10
Output:10
Explanation:

Only 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.