C# Solution, O(n) Time


  • 0
    S
    public int FindPoisonedDuration(int[] timeSeries, int duration) {
            int result = 0;
    	int previousDuration = 0;
    	if (timeSeries == null || timeSeries.Length == 0) return result;
    
    	for (var i = 0; i < timeSeries.Length; i++)
    	{
    		if (i == 0 || timeSeries[i] >= previousDuration)
    		{
    			result += duration;
    		}
    		else
    		{
    			result += timeSeries[i] + duration - previousDuration;
    		}
    		previousDuration = timeSeries[i] + duration;
    	}
    
    	return result;
        }
    
    

Log in to reply
 

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