Simple Java O(n) solution, easy to understand

  • 1

    Keep track the end of poison status caused by previous attacks.

        public int findPosisonedDuration(int[] timeSeries, int duration) {
            int result = 0;
    //The end of previous poison status
            int pre = Integer.MIN_VALUE;
            for(int t: timeSeries) {
                int end = t + duration;
                result += end - Math.max(t, pre);
                pre = end;
            return result;

Log in to reply

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