Simple python solution


  • 0
    K

    The tricky part is how to prevent double counting, which occurs if we simply add the duration to the total poisoned time. To avoid this, one can use a flag that indicates when the poisoned time ends. Using this flag, we can calculate and subtract the double-counted time properly.

    def findPosisonedDuration(self, timeSeries, duration):
        """
        :type timeSeries: List[int]
        :type duration: int
        :rtype: int
        """
        p_time = 0
        p_end = 0
        
        for i in xrange(len(timeSeries)):
            p_time += duration - max(0, p_end - timeSeries[i])
            p_end = timeSeries[i] + duration
            
        return p_time

Log in to reply
 

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