int candy(vector<int> &ratings)
{
int size=ratings.size();
if(size<=1)
return size;
vector<int> num(size,1);
for (int i = 1; i < size; i++)
{
if(ratings[i]>ratings[i1])
num[i]=num[i1]+1;
}
for (int i= size1; i>0 ; i)
{
if(ratings[i1]>ratings[i])
num[i1]=max(num[i]+1,num[i1]);
}
int result=0;
for (int i = 0; i < size; i++)
{
result+=num[i];
// cout<<num[i]<<" ";
}
return result;
}
A simple solution


Good solution, same as mine in Java.
—————————————————————
The upper was my first comment, and gained many 1.
So I post my java code as the second comment.
This is my third comment, to explain .
As some guys want to see my code, see below please.
public int candy(int[] ratings) { int len = ratings.length; int[] candy = new int[len]; candy[0] =1; for (int i = 1; i < len; ++i) { if (ratings[i] > ratings[i1]) { candy[i] = candy[i1] + 1; } else { candy[i] = 1; } } int total = candy[len1]; for (int i = len  2; i >= 0; i) { if (ratings[i] > ratings[i+1] && candy[i] <= candy[i+1]) { candy[i] = candy[i+1] + 1; } total += candy[i]; } return total; }