Very easy to understand O(n) Java Solution. 1 significant line.

  • 0

    Please find the explanation in the code

    public class Solution {
        public int findPoisonedDuration(int[] timeSeries, int duration) {
           if(timeSeries.length==0 || timeSeries==null) return 0;
           int sum = duration; // This is because the last entry in the array will always last for the entire duration
           for(int i=0;i<timeSeries.length-1;i++) // Since we already considered the duration for the last element in the array in sum, we need not consider the last entry of the array.
                   sum+=Math.min((timeSeries[i+1]-timeSeries[i]),duration); // Checks the minimum among two : either the duration or the distance between the next and the current element. This is because if there is an attack before the previous attack is completed, the previous attack is cancelled.
            return sum;

Log in to reply

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