-
Notifications
You must be signed in to change notification settings - Fork 0
/
Candy.cpp
61 lines (37 loc) · 837 Bytes
/
Candy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
http://www.cnblogs.com/felixfang/p/3620086.html
class Solution {
public:
int candy(vector<int> &ratings) {
int length = 0; //the descent length
int total = 0; //the total number of candy
int beforeDesc = 1; //the count of candy before desc
int preCnt = 1; //the pre count of candy
if(ratings.begin() != ratings.end()){
total ++;
for(vector<int>::iterator i = ratings.begin(); i != ratings.end(); i++){
if(*i < *(i - 1)){
length ++;
if( beforeDesc <= length){
total ++;
}
total += length;
preCnt = 1;
}
else{
int curCnt = 0;
if( *i > *(i - 1)){
curCnt = preCnt + 1;
}
else{
cutCnt = 1;
}
total += cutCnt;
beforeDesc = curCnt;
preCur = curCnt;
length = 0;
}
}
}
return total;
}
};