Overlapping Illness Periods

EasyArrayMathSortingStack

Description

You need to calculate the total duration of an illness. Given an array of integers timeSeries (sorted in ascending order) representing the times at which the illness starts, and an integer duration representing how many seconds each illness lasts, return the total number of seconds the person is sick. Key rule: If a new illness starts before the previous one ends, the illness timer resets to the full duration from the new start time (illnesses do not stack, they restart). Example: If illness starts at time 1 and lasts 3 seconds, the person is sick from time 1 to 4. If a new illness starts at time 2, the new illness extends from time 2 to 5 (the timer resets). If another illness starts at time 6, that's a separate illness. Your function will receive solve(timeSeries, duration) and should return the total number of seconds the person is sick.

Examples

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

Illness starts at time 1, lasts 2 seconds (covers times 1-2). Next illness starts at time 4, lasts 2 seconds (covers times 4-5). Total sick time: 2 + 2 = 4 seconds.

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

Illness starts at time 1 (would end at time 4). New illness at time 2 (resets timer, would end at time 5). New illness at time 3 (resets timer, would end at time 6). New illness at time 4 (resets timer, would end at time 7). The person is sick from time 1 to 7, which is 6 seconds total.

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

Single illness starts at time 5 and lasts 10 seconds, so the person is sick from time 5 to 15. Total: 10 seconds.

Constraints

  • 1 ≤ timeSeries.length ≤ 10^4
  • 0 ≤ timeSeries[i], duration ≤ 10^7

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!