-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_135_Candy.java
More file actions
38 lines (34 loc) · 1.73 KB
/
Copy pathLeetCode_135_Candy.java
File metadata and controls
38 lines (34 loc) · 1.73 KB
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
class Solution {
public int candy(int[] ratings) {
int sum = 1; // Initialize sum to 1 to account for the first child receiving at least one candy
int i = 1; // Start from the second child
// Iterate through the ratings array
while (i < ratings.length) {
// If the current child's rating is equal to the previous child's rating
if (ratings[i] == ratings[i - 1]) {
sum++; // Increment sum for the current child
i++; // Move to the next child
continue; // Continue to the next iteration
}
int peak = 1; // Initialize peak for the increasing sequence of ratings
// Count the number of children with increasing ratings
while (i < ratings.length && ratings[i] > ratings[i - 1]) {
peak++; // Increment peak for each child with a higher rating
sum += peak; // Add the number of candies for the current peak
i++; // Move to the next child
}
int down = 1; // Initialize down for the decreasing sequence of ratings
// Count the number of children with decreasing ratings
while (i < ratings.length && ratings[i] < ratings[i - 1]) {
sum += down; // Add the number of candies for the current down
down++; // Increment down for the next child
i++; // Move to the next child
}
// If the down sequence is longer than the peak sequence
if (down > peak) {
sum += down - peak; // Adjust the sum to account for the excess candies needed
}
}
return sum; // Return the total number of candies distributed
}
}