Python Solution


  • 0
    N
    class Solution(object):
        def findPoisonedDuration(self, timeSeries, duration):
            if not timeSeries or duration == 0:
                return 0
                
            total_duration = 0
            pre_time = timeSeries[0]
            for time in timeSeries:
                total_duration += min(duration, time - pre_time)
                pre_time = time
                
            total_duration += duration
            
            return total_duration
    

    Let's try to make an one-line version,

    class Solution(object):
        def findPoisonedDuration(self, timeSeries, duration):
            return sum(min(cur - pre, duration) for pre,cur in zip(timeSeries, timeSeries[1:] + [float('inf')]))
    

Log in to reply
 

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