-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCandy.cpp
33 lines (20 loc) · 1014 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
/*
Solution by Rahul Surana
***********************************************************
There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
***********************************************************
*/
class Solution {
public:
int candy(vector<int>& ratings) {
int n = ratings.size(), ind = 0, mv = 1e9;
vector<int> p(n,1);
for(int i = 1; i < n; i++) { if(ratings[i] > ratings[i-1]) p[i] = p[i-1] + 1; }
for(int i = n-2; i >= 0; i--) if(ratings[i] > ratings[i+1]) if(p[i] < p[i+1]+1) p[i] = p[i+1] + 1;
return accumulate(p.begin(),p.end(),0);
}
};