Java 10 lines Simple Solution O(n)


  • 0
    C
    public int findPoisonedDuration(int[] timeSeries, int duration) {
            int sum = 0;
            if (timeSeries.length == 0) return sum;
            for (int i = 0; i < timeSeries.length - 1; i++) {
                int begin = timeSeries[i];
                int end = timeSeries[i+1];
                if (end - begin < duration) sum += end - begin;
                else sum += duration;
    
            }
            return sum + duration;
        }
    

Log in to reply
 

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