-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCandy.cpp
More file actions
40 lines (35 loc) · 1.21 KB
/
Copy pathCandy.cpp
File metadata and controls
40 lines (35 loc) · 1.21 KB
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
class Solution {
public:
int candy(vector<int> &ratings) {
int mincandies = 0;
int *pcandies = new int[ratings.size()];
for(int i=0;i<ratings.size();++i){
pcandies[i]=0;
}
int i=0;
while(i<ratings.size()){
for(int k=i;k<ratings.size();++k){
if((k+1)<ratings.size() && ratings[k]>ratings[k+1]) continue;
else{
pcandies[k]=1;
for(int j=k-1;j>=i;--j){
if(ratings[j]>ratings[j+1]) pcandies[j]=pcandies[j+1]+1;
else pcandies[j]=pcandies[j+1];
}
if(i-1>=0 && pcandies[i]<=pcandies[i-1]){
if(ratings[i]>ratings[i-1]) pcandies[i]=pcandies[i-1]+1;
for(int j=i+1;j<=k;++j){
if(ratings[j]>ratings[j-1]) pcandies[j]=pcandies[j-1]+1;
}
}
i=k+1;
}
}
}
for(int i=0;i<ratings.size();++i){
mincandies += pcandies[i];
}
delete []pcandies;
return mincandies;
}
};