I hate teemo - Java O(n) AC


  • 0
    J

    As I traverse the array I keep track of the previous start of duration. And update the total time depending on the current time.

    public class Solution {
        public int findPoisonedDuration(int[] timeSeries, int duration) {
            if(timeSeries.length == 0) return 0;
            
            int start = timeSeries[0]; 
            int total = 0;
            
            for(int i = 1; i < timeSeries.length; ++i){
                int curr = timeSeries[i];
                if(curr - start >= duration)
                    total += duration;
                else
                    total += curr - start;
        
                start = curr;
            }
            
            return total + duration;
        }
    }
    

Log in to reply
 

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