Java solution


  • 0

    [Please ignore the method name spelling since LeetCode gave this]

    public class Solution {
        public int findPosisonedDuration(int[] timeSeries, int duration) {
            
            int total_poisoned_time = 0,
                next_start_time = 0;
                
                
            for(int i=0;i<timeSeries.length;++i)
            {
                if(timeSeries[i] == 0 || timeSeries[i] - next_start_time > 0)
                {
                    next_start_time = timeSeries[i] + duration - 1;
                    total_poisoned_time += duration;
                }
                else
                {
                    total_poisoned_time += (timeSeries[i] + duration - 1) - next_start_time; // since overlapping happens , we calculate the offset before judging next start time for poisoning state 
                    next_start_time = timeSeries[i] + duration - 1;
                }
            }
            
            return total_poisoned_time;
        }
    }
    

Log in to reply
 

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