class Solution {
public:
int candy(vector<int> &ratings) {
int m = ratings.size();
int sum = 0;
vector<int> candy(m);
candy[0] = 1;
for(int i = 1; i < m; i++){
if (ratings[i] > ratings[i - 1])
candy[i] = candy[i - 1] + 1;
else
candy[i] = 1;
} //这个循环结束,满足每个位置大数的candies都大于其左邻居
sum = candy[m - 1];
for(int i = m - 2; i >= 0; i--){
if(ratings[i] > ratings[i + 1])
candy[i] = max(candy[i], candy[i + 1] + 1);
sum += candy[i];
}
return sum;
}
};
LeetCode :: Candy
最新推荐文章于 2019-07-27 09:58:00 发布