C# - 5 lines O(n)


  • 0

    track when the poison wears off (ends). For the next time you only count the portion of the duration which is after the end of the previous poisoning.

        public int FindPoisonedDuration(int[] timeSeries, int duration) 
        {
            int cnt = 0, end = 0;
            foreach (int x in timeSeries)
            {
                cnt += end > x ? duration - (end - x) : duration;
                end = x + duration;
            }
            return cnt;
        }
    

Log in to reply
 

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