C++ 4 line solution with explanation


  • 0
    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
    	int total = 0;
    	for (int i = 0; i < timeSeries.size(); i++) 
            total += (i > 0 && timeSeries[i] - timeSeries[i-1] < duration ? timeSeries[i] - timeSeries[i - 1] : duration);
    	return total;
    }
    

    At each stage, we check if the difference between current time and last time is bigger than duration. If so, we add duration. If not, we add the difference between two time stamps


Log in to reply
 

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