Share my Java Solution.O(n)


  • 0
    L

    Maybe my code is not so nice, but it's easy to understand.

    public int findPoisonedDuration(int[] timeSeries, int duration) {
            if(timeSeries == null || timeSeries.length == 0) return 0;
            int n = timeSeries.length;
            int currentAttackLasts = 0, noAttacking = 0;
            for(int i = 0; i < n; i++){
                currentAttackLasts = timeSeries[i] + duration;
                if(i < n - 1 && currentAttackLasts < timeSeries[i + 1])
                    noAttacking += timeSeries[i + 1] - currentAttackLasts;
            }
            return currentAttackLasts - timeSeries[0] - noAttacking;
        }
    

Log in to reply
 

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