Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly duration seconds. More formally, an attack at second t will mean Ashe is poisoned during the inclusive time interval [t, t + duration - 1]. If Teemo attacks again before the poison effect ends, the timer for it is reset, and the poison effect will end duration seconds after the new attack.

You are given a non-decreasing integer array timeSeries, where timeSeries[i] denotes that Teemo attacks Ashe at second timeSeries[i], and an integer duration.

Return the total number of seconds that Ashe is poisoned.

Example 1:

Input: timeSeries = [1,4], duration = 2 Output: 4 Explanation: Teemo's attacks on Ashe go as follows: - At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2. - At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5. Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.

Example 2:

Input: timeSeries = [1,2], duration = 2 Output: 3 Explanation: Teemo's attacks on Ashe go as follows: - At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2. - At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3. Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.

Constraints:

  • 1 <= timeSeries.length <= 104
  • 0 <= timeSeries[i], duration <= 107
  • timeSeries is sorted in non-decreasing order.

Note: This problem is from LeetCode.
βœ… Compiled
βœ… Executed
❌ Correct
program main
    implicit none

    integer, parameter :: n = 2
    integer, dimension(n) :: timeSeries = (/ 1, 4 /)
    integer, parameter :: duration = 2
    integer :: i

    write (*, '(A)') 'Total seconds Ashe is poisoned:'
    do i = 1, n
        write (*, '(I0)') solve(timeSeries(i), duration)
    end do

contains

    function solve(time, dur) result(res)
        implicit none
        integer, intent(in) :: time, dur
        integer :: res

        if (time < dur) then
            res = dur
        else
            res = time + dur - 1
        end if
    end function solve
end program main
❌ Compiled
❌ Executed
❌ Correct
module poisoned_ashe
    implicit none
    integer, parameter :: dp = selected_real_kind(15, 307) ! double precision

contains

    function solve(timeSeries, duration) result(totalPoisonedTime)
        integer, intent(in) :: timeSeries(:), duration
        integer :: totalPoisonedTime, currentTime, previousTime

        currentTime = 0
        previousTime = 0
        totalPoisonedTime = 0

        do i = 1, size(timeSeries)
            currentTime = timeSeries(i)
            if (currentTime - previousTime < duration) then
                ! Poison effect is still active, reset the timer
                totalPoisonedTime = max(totalPoisonedTime, currentTime + duration)
            else
                ! Poison effect has ended, add the current time to the total
                totalPoisonedTime = totalPoisonedTime + currentTime
            end if
            previousTime = currentTime
        end do

        totalPoisonedTime = totalPoisonedTime + currentTime

    end function solve

end module poisoned_ashe

program test
    use poisoned_ashe
    implicit none
    integer, parameter :: timeSeries(2) = [1, 4]
    integer :: duration = 2
    integer :: totalPoisonedTime

    totalPoisonedTime = solve(timeSeries, duration)
    write (*,*) "Total poisoned time:", totalPoisonedTime

    duration = 2
    totalPoisonedTime = solve(timeSeries, duration)
    write (*,*) "Total poisoned time:", totalPoisonedTime

    duration = 2
    totalPoisonedTime = solve([1, 2], duration)
    write (*,*) "Total poisoned time:", totalPoisonedTime

end program test
🌐 Data from online sources
def find_poisoned_duration(time_series, duration):
    if not time_series:
        return 0
    total = 0
    for i in range(1, len(time_series)):
        total += min(time_series[i] - time_series[i - 1], duration)
    return total + duration

Iterate through the timeSeries array and for each element, calculate the difference between the current element and the previous element. If the difference is less than duration, add that difference to the total time poisoned; otherwise, add the duration to the total time poisoned. In the end, add one more duration to the total to account for the last attack.

By doing this, we ensure that we only count the poisoned time without overlapping, since consecutive poisoned times will add up to the original duration.

🌐 Data from online sources
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
    if (timeSeries.empty()) return 0;
    int total = 0;
    for (int i = 1; i < timeSeries.size(); ++i) {
        total += min(timeSeries[i] - timeSeries[i - 1], duration);
    }
    return total + duration;
}

Iterate through the timeSeries array and for each element, calculate the difference between the current element and the previous element. If the difference is less than duration, add that difference to the total time poisoned; otherwise, add the duration to the total time poisoned. In the end, add one more duration to the total to account for the last attack.

By doing this, we ensure that we only count the poisoned time without overlapping, since consecutive poisoned times will add up to the original duration.