Easy to Understand O(n) Python Solution

  • 0

    The solution is simple, use a variable cend to store the current end time of the poisoned status. Traverse the array, if the start time is less than the current end time, subtract the overlapping time and add to the sum. Else just simply add the duration to the sum.

    class Solution(object):
        def findPoisonedDuration(self, timeSeries, duration):
            :type timeSeries: List[int]
            :type duration: int
            :rtype: int
            if len(timeSeries) == 0:
                return 0
            cend = timeSeries[0] + duration
            sum = duration
            for i in range(1, len(timeSeries)):
                if timeSeries[i] < cend:
                    sum += timeSeries[i] + duration - cend
                    sum += duration
                cend = timeSeries[i] + duration
            return sum

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.