Easy Java O(n) solution


  • 0
    Z
    public class Solution {
        public int findPoisonedDuration(int[] timeSeries, int duration) {
            if(timeSeries.length == 0)
                return 0;
            int poisonEnd = -1;
            int res = 0;
            for(int x : timeSeries){
                if(x <= poisonEnd){
                    res += duration - (poisonEnd - x + 1);
                } else {
                    res += duration;
                }
                poisonEnd = x + duration - 1;
            }
            return res;
        }
    }
    

Log in to reply
 

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