Failed for case [0], but passed in Xcode


  • 0
    R
    int candy(vector<int> &ratings) {
    	if(ratings.size() == 0) return 0;
    	vector<int> candies;
    	candies.push_back(0);
    	for (int i = 1; i < ratings.size(); i++)
    	{
    		if(ratings[i] > ratings[i-1])
    			candies.push_back(candies[i-1] + 1);
    		else if(ratings[i] < ratings[i-1])
    			candies.push_back(candies[i-1] - 1);
    		else
    			candies.push_back(candies[i-1]);
    	}
    	int min=0;
    	for(int i=0; i<candies.size(); i++)
    		if(candies[i]<min)
    			min = candies[i];
    	int res;
    	for(int i=0; i<candies.size(); i++)
    		res += candies[i] + (1-min);
    	
    	return res;
    }
    

    Thanks!


Log in to reply
 

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