Easy to Read Python Solution o(n) 72 ms


  • 0

    I felt like the other solutions are less readable so I am offering this solution. The plus side is this only iterates through the array once rather than twice (unlike most other solutions).

    def findPoisonedDuration(self, timeSeries, duration):
            """
            :type timeSeries: List[int]
            :type duration: int
            :rtype: int
            """
            if not timeSeries:
                return 0
            
            total = 0
            start = timeSeries[0]
            end = start + duration
            
            for i in xrange(1, len(timeSeries)):
                time = timeSeries[i]
                if time > end:
                    total += end-start  # add to total and reset start
                    start = time
                end = time + duration
            
            total += end - start  # must account for final time in timeSeries
            return total
    

Log in to reply
 

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