当前位置: 代码迷 >> 综合 >> leetcode 135. Candy (hard)
  详细解决方案

leetcode 135. Candy (hard)

热度:60   发布时间:2024-01-05 01:11:57.0

原题
前后两遍遍历

class Solution
{
    public:int candy(vector<int> &ratings){
    int len = ratings.size();vector<int> candys(len, 1);for (int i = 1, base = 2; i < len; i++){
    if (ratings[i] > ratings[i - 1])candys[i] = max(base++, candys[i]);elsebase = 2;}for (int i = len - 2, base = 2; i >= 0; i--){
    if (ratings[i] > ratings[i + 1])candys[i] = max(base++, candys[i]);elsebase = 2;}for (auto c : candys){
    cout << c << endl;}return accumulate(candys.begin(), candys.end(), 0);}
};