标签:style blog color io for div sp log on
题目大概就是考察,如何找到山峰的峰值。从左到右扫一遍,再从右到左扫一遍,确定每个位置的峰值。
1 class Solution { 2 public: 3 int Max(int a,int b) 4 { 5 return a>b?a:b; 6 } 7 int candy(vector<int> &ratings) { 8 if(ratings.size()==0) 9 return 0; 10 if(ratings.size()==1) 11 return 1; 12 13 int num = ratings.size(); 14 int cnt = 1; 15 int sum = 0; 16 vector<int> v(num,1); 17 for(int i=0;i<num-1;i++) 18 { 19 if(ratings[i]<ratings[i+1]) 20 v[i+1] = ++cnt; 21 else 22 cnt =1; 23 } 24 25 cnt = 1; 26 for(int j=num-1;j>0;j--) 27 { 28 if(ratings[j]<ratings[j-1]) 29 v[j-1] = Max(++cnt,v[j-1]); 30 else 31 cnt = 1; 32 } 33 for(int i=0;i<num;i++) 34 sum += v[i]; 35 36 return sum; 37 } 38 };
标签:style blog color io for div sp log on
原文地址:http://www.cnblogs.com/ZhangYushuang/p/3958892.html