forked from garvit-bhardwaj/Leetcode-Problems-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandy
More file actions
22 lines (22 loc) · 576 Bytes
/
Candy
File metadata and controls
22 lines (22 loc) · 576 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size();
vector<int> candies(n,1);
int min = 0;
for(int i=0; i<n-1; i++){
if(ratings[i] < ratings[i+1]){
candies[i+1] = candies[i] + 1;
}
}
for(int i=n-1; i>0; i--){
if(ratings[i-1] > ratings[i] && candies[i-1]<=candies[i]){
candies[i-1] = candies[i] + 1;
}
}
for(int i=0; i<n; i++){
min += candies[i];
}
return min;
}
};