Easy to understand Java solution

  • 6

    We just need to take the minimum value between the difference of previous timestamp and the current timestamp, and the duration.
    This makes sense because the poisoning will happen at each timestamp.
    At the end, I am adding the duration to the total answer to take in consideration the poisoning at the last timestamp.

    public class Solution {
        public int findPosisonedDuration(int[] timeSeries, int duration) {
            if(timeSeries.length == 0)return 0;
            if(timeSeries.length == 1)return duration;
            int total = 0;
            for(int i=1; i<timeSeries.length;i++)
                total += Math.min(duration,timeSeries[i]-timeSeries[i-1]);
            total += duration;
         return total;   

Log in to reply

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