forked from PawanJaiswal08/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandy.cpp
More file actions
28 lines (24 loc) · 786 Bytes
/
Candy.cpp
File metadata and controls
28 lines (24 loc) · 786 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
28
class Solution {
public:
int candy(vector<int>& ratings) {
int n= ratings.size();
int ans = 0;
//students who get more candies than its left neighbour
vector<int>L_candy(n,1);
for(int i=1; i<n; i++){
if(ratings[i]>ratings[i-1] )
L_candy[i]=L_candy[i-1]+1;
}
//student who get more candies than its right neighbour
vector<int> R_candy(n,1);
for(int i=n-2; i>=0; i--){
if(ratings[i]>ratings[i+1] )
R_candy[i] = R_candy[i+1]+1;
}
// maximum which staisfy bothe left and right neighbour
for(int i=0; i<n; i++){
ans += max(L_candy[i], R_candy[i]);
}
return ans;
}
};