6 lines of Java O(n) using Math.min


  • 4
    public class Solution {
        /*
        if time.length == 0: return 0
        for i = 1...n
            diff = time[i] - time[i-1]
            result += diff > duration ? duration : diff
        result += duration
        */
        public int findPoisonedDuration(int[] timeSeries, int duration) {
            if (timeSeries.length == 0) return 0;
            int result = 0;
            for (int i = 1; i < timeSeries.length; i += 1) 
                result += Math.min(timeSeries[i] - timeSeries[i-1], duration);
            result += duration;
            return result;
    
    }
    

    }


Log in to reply
 

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