-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCandy.cpp
32 lines (32 loc) · 841 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
class Solution {
public:
void print(vector<int>& c){
for(int i=0; i<c.size(); i++)
cout<<c[i]<<",";
cout<<endl;
}
int candy(vector<int>& ratings) {
int n=ratings.size();
int ans = 1;
int up = 0, down = 0, peak = 0;
for (int i = 1; i <n; i++) {
//ascending
if (ratings[i-1] < ratings[i]) {
peak = ++up;
down = 0;
ans+= 1+up;
}
//slope=0
else if (ratings[i-1] == ratings[i]) {
peak =up = down = 0;
ans++;
}
else {//decreasing
up = 0;
down++;
ans += down + (peak < down);
}
}
return ans;
}
};