Easy to understand Java solution, simpler than a few top solutions here


  • 0
    A
    public class Solution {
        public int findPoisonedDuration(int[] timeSeries, int duration) {
            // algorithm 2017/05/31: simply add up, each time we either add 'duration', or add the delta of time series, whichever smaller
            if (null == timeSeries || 0 == timeSeries.length) {
                return 0;
            }
        
            // kick start with the first attack
            int result = duration;
            for (int index = 1; index < timeSeries.length; index++) {
                result += Math.min(duration, timeSeries[index] - timeSeries[index-1]);
            }
            return result;
        }
    }

Log in to reply
 

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