O(n) easy understand Python solution


  • 0
    L

    The basic idea is to calculate the overlap of adjacent two time series.

    class Solution(object):
        def findPoisonedDuration(self, timeSeries, duration):
            """
            :type timeSeries: List[int]
            :type duration: int
            :rtype: int
            """
            c, pre_end = 0, -1
            for i in range(0, len(timeSeries)):
                current_start, current_end = timeSeries[i], timeSeries[i] + duration - 1
                if current_start > pre_end:
                    c += duration
                else:
                    duplicate_span = pre_end - current_start + 1
                    c = c + duration - duplicate_span
                pre_end = current_end
            return c
    

Log in to reply
 

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