Simple Java solution


  • 0
    N

    The idea is to get the maximum poisoned minutes possible by calculating number of attacks * duration and then subtracting any overlaps in situations where subsequent attacks are made before the duration has elapsed.

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

Log in to reply
 

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