-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcandy.cpp
38 lines (32 loc) · 890 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
34
35
36
37
38
/*
https://leetcode.com/problems/candy/submissions/1093379850?envType=study-plan-v2&envId=top-interview-150
Runtime
Details
14ms
Beats 79.70%of users with C++
Memory
Details
17.96MB
Beats 64.73%of users with C++
*/
class Solution {
public:
int candy(vector<int> &ratings) {
int n = ratings.size();
vector<int> candies(n, 1); // Start with one candy for each child
// Left-to-Right pass
for (int i = 1; i < n; i++) {
if (ratings[i] > ratings[i - 1]) {
candies[i] = candies[i - 1] + 1;
}
}
// Right-to-Left pass
for (int i = n - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
candies[i] = max(candies[i], candies[i + 1] + 1);
}
}
// Sum up the candies
return accumulate(candies.begin(), candies.end(), 0);
}
};