点击打开链接
class Solution {
public:
int candy(vector<int>& ratings) {
int len = ratings.size();
vector<int> res(len);
for(int i=1, inc =1; i<len; i++){
if(ratings[i] > ratings[i-1]){
res[i] = max(inc++, res[i]);
}
else{
inc = 1;
}
}
for(int i=len-2, inc = 1; i>=0; i--){
if(ratings[i] > ratings[i+1]){
res[i] = max(inc++, res[i]);
}
else{
inc = 1;
}
}
return accumulate(res.begin(), res.end(), len);
}
};