s4553711
11/23/2017 - 2:33 PM

135.cpp

class Solution {
public:
    int candy(vector<int>& ratings) {
        vector<int> candys;
        candys.resize(ratings.size(), 1);
        for(int i = 1; i < ratings.size(); i++) {
            if (ratings[i] > ratings[i-1]) {
                candys[i] = candys[i-1] + 1;
            }
        }
        for(int j = ratings.size() - 2; j >= 0; j--) {
            if (ratings[j] > ratings[j+1] && candys[j] <= candys[j+1]) {
                candys[j] = candys[j+1] + 1;
            }
        }
        int n = 0;
        for(int i = 0; i < candys.size(); i++) {
            n += candys[i];
        }
        return n;
    }
};