左右扫最长上升子序列即可
/*
* @lc app=leetcode id=135 lang=cpp
*
* [135] Candy
*/
// @lc code=start
class Solution {
public:
int candy(vector<int>& ratings) {
int N = ratings.size();
vector<int> L(N,1);
vector<int> R(N,1);
for(int i=1;i<N;i++){
if(ratings[i] > ratings[i-1]) L[i] = L[i-1]+1;
}
for(int i=N-2;i>=0;i--){
if(ratings[i] > ratings[i+1]) R[i] = R[i+1]+1;
}
int ans = 0;
for(int i=0;i<N;i++){
ans += max(L[i],R[i]);
}
return ans;
}
};
// @lc code=end