simple O(n) c++ solution


  • 0
    int findPoisonedDuration(vector<int>& timeSeries, int duration) 
    {
        int result = duration;  // just for last value
        for (int i = 0; i < (int)(timeSeries.size() - 1); i++)
        {
            result += min(duration, timeSeries[i + 1] - timeSeries[i]);
        }
        
        return timeSeries.empty() ? 0 : result;
    }

Log in to reply
 

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