-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandy.cpp
More file actions
27 lines (27 loc) · 848 Bytes
/
candy.cpp
File metadata and controls
27 lines (27 loc) · 848 Bytes
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
class Solution {
public:
int candy(vector<int>& ratings) {
if (ratings.size() <= 1) return ratings.size();
int left=0, right=0, pos=0, n=ratings.size(), sum=0;
while(left < n) {
int i, lval = 1, rval;
if(left!=0 && ratings[left]>ratings[left-1]) lval++;
for(i = left+1; i<n && ratings[i]>ratings[i-1]; i++) {// gradient increase
sum += lval;
if (ratings[i] > ratings[i-1]) lval++;
}
pos = --i; // find local max
for(right = i+1; right<n && ratings[right]<ratings[right-1]; right++); // gradient decrease
right--; // right point to the last local decrease element
for(i = right, rval = 1; i > pos; i--) {
sum += rval;
if (ratings[i] < ratings[i-1]) {
rval++;
}
}
sum += max(lval, rval);
left = right+1;
}
return sum;
}
};