C# - only count time beyond marker of already counted time


  • 0

    The idea is simple. Keep a marker at the position for which you have already counted time. When you get the next time (they are conveniently sorted for you already) only count the amount of the duration which is beyond the already accounted for time line.

        public int FindPoisonedDuration(int[] timeSeries, int duration) 
        {
            int sum = 0;
            int right = 0;
            
            foreach (int time in timeSeries)
            {
                if (time > right) sum += duration;
                else sum += time + duration - right;
                
                right = time + duration;
            }
            
            return sum;
        }
    

Log in to reply
 

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