Skip to content

Latest commit

 

History

History
20 lines (20 loc) · 498 Bytes

135. Candy.md

File metadata and controls

20 lines (20 loc) · 498 Bytes
class Solution {
public:
    int candy(vector<int>& ratings) {
        int n = ratings.size(), sum = 0;
        vector<int> v(n, 1);
        for (int i = 1; i < n; ++i) {
            if (ratings[i] > ratings[i - 1])
                v[i] = v[i - 1] + 1;
        }
        for (int i = n - 1; i > 0; --i) {
            if (ratings[i - 1] > ratings[i])
                v[i - 1] = max(v[i - 1], v[i] + 1);
        }
        for (int num : v)
            sum += num;
        return sum;
    }
};