We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 1089be2 + 0478eea commit d79de5aCopy full SHA for d79de5a
135. Candy.cpp
@@ -0,0 +1,24 @@
1
+class Solution {
2
+public:
3
+ int candy(vector<int>& ratings) {
4
+ int n = ratings.size();
5
+ vector<int> count(n, 1); // Step 1: Initialize with 1
6
+
7
+ // Step 2: Left to Right
8
+ for (int i = 1; i < n; i++) {
9
+ if (ratings[i] > ratings[i - 1]) {
10
+ count[i] = count[i - 1] + 1;
11
+ }
12
13
14
+ // Step 3: Right to Left
15
+ for (int i = n - 2; i >= 0; i--) {
16
+ if (ratings[i] > ratings[i + 1]) {
17
+ count[i] = max(count[i], count[i + 1] + 1);
18
19
20
21
+ // Step 4: Total candies
22
+ return accumulate(count.begin(), count.end(), 0);
23
24
+};
0 commit comments