题目:
题解:
cpp
int candy(int* ratings, int ratingsSize) {
int ret = 1;
int inc = 1, dec = 0, pre = 1;
for (int i = 1; i < ratingsSize; i++) {
if (ratings[i] >= ratings[i - 1]) {
dec = 0;
pre = ratings[i] == ratings[i - 1] ? 1 : pre + 1;
ret += pre;
inc = pre;
} else {
dec++;
if (dec == inc) {
dec++;
}
ret += dec;
pre = 1;
}
}
return ret;
}