Java Solution using hashmap

  • 0
    public class Solution {
        public int findPoisonedDuration(int[] timeSeries, int duration) {
            if(timeSeries.length==0) return 0;
            HashMap<Integer,Integer> h = new HashMap<Integer,Integer>();
            for(int i=1;i<timeSeries.length;i++){
                int timeDif=timeSeries[i]-timeSeries[i-1];
                if(timeDif<duration) h.put(timeSeries[i],h.get(timeSeries[i-1])+timeDif);
                else h.put(timeSeries[i],h.get(timeSeries[i-1])+duration);
            return h.get(timeSeries[timeSeries.length-1]);

Log in to reply

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