C++ O(n) time and O(1) space


  • 0
    D

    Sum the min of each interval and duration.

    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
            if (timeSeries.empty() || duration == 0) return 0;
            int total = 0;
            int precursor = timeSeries[0];
            for (auto val : timeSeries) {
                total += min(duration, val - precursor);
                precursor = val;
            }
            return total + duration;
        }
    

  • 0

    I think you meant O(1) space in your title:-)


  • 0
    D

    @zzg_zzm You're correct. Thanks.


Log in to reply
 

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