Why I cannot get accept ?


  • -2
    I
    int pre_candy = 0,
    	curr_candy = 0,
    	result = 0,
    	pre = 0,
    	size = ratings.size();
    	if(size == 1)
    	return 1;
    if(ratings[0]<=ratings[1])
    {
    	result = 1;
    	pre_candy = 1;
    }
    else
    {
    	int index = 1;
    	result = 2;
    	while(index<size-1&&ratings[index] > ratings[index+1])
    	{
    		result++;
    		index++;
    	}
    }
    for(int i = 1;i<size-1;i++)
    {
    	if(ratings[i]<=ratings[i+1]&&ratings[i]<=ratings[i-1])//V
    	{
    		pre_candy = 1;
    		result += 1;
    	}
    	else if(ratings[i]<ratings[i+1]&&ratings[i]>ratings[i-1])// /
    	{
    		pre_candy += 1;
    		result += pre_candy;
    	}
    	else if(ratings[i]>=ratings[i+1]&&ratings[i]>ratings[i-1])// 
    	{
    	pre_candy += 1;
    	int index = i;
    	curr_candy =1;
    	while(index<size-1&&ratings[index] > ratings[index+1])
    	{
    		curr_candy++;
    		index++;
    	}
    	pre_candy = pre_candy > curr_candy ? pre_candy : curr_candy;
    		result += pre_candy;
    		pre = i;
    	}
    	else if(ratings[i]>=ratings[i+1]&&ratings[i]<=ratings[i-1])// 
    	{
    		pre_candy = i - pre + 1;
    		result += pre_candy;
    	}
    }
    
    if(ratings[size -2]<ratings[size-1])
    {
    	pre_candy += 1;
    	result += pre_candy;
    }
    else
    	result += 1;
    return result;

Log in to reply
 

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