Leetcode-135 Candy(分发糖果)
1 class Solution 2 { 3 public: 4 int candy(vector<int>& ratings) 5 { 6 int result = 0; 7 vector<int> v1 (ratings.size(),1); 8 vector<int> v2 (ratings.size(),1); 9 for(int i = 1;i < ratings.size();i ++) 10 { 11 if(ratings[i]>ratings[i-1]) 12 v1[i] = v1[i-1]+1; 13 } 14 for(int i = ratings.size()-2;i >= 0;i --) 15 { 16 if(ratings[i]>ratings[i+1]) 17 v2[i] = v2[i+1]+1; 18 } 19 20 for(int i = 0;i < ratings.size();i ++) 21 result += max(v1[i],v2[i]); 22 return result; 23 } 24 };