Self Explanatory Code in Java, Very simple and Meaningful variable names to understand


  • 0
    R
    public int findPoisonedDuration(int[] timeSeries, int duration) {
            
            if(timeSeries.length == 0){
                return 0;
            }
            
            int currentIndex = 0;
            int totalPoisonTime = 0;
            int maxReachable = 0;
            
            maxReachable = timeSeries[0] + duration-1;
            totalPoisonTime = duration;
            
            for(int i=1;i < timeSeries.length;i++){
                
                int currentReachable = timeSeries[i] + duration -1 ;
                
                if(timeSeries[i] > maxReachable){
                    totalPoisonTime = totalPoisonTime + duration;
                }else{
                    totalPoisonTime = totalPoisonTime + (currentReachable - maxReachable);
                }
                
                if(currentReachable > maxReachable){
                    maxReachable = currentReachable;
                }
                
            }
            
            return totalPoisonTime;
        }
    

Log in to reply
 

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