- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandyLeetcode.java
More file actions
Latest commit
28 lines (23 loc) · 700 Bytes
/
Copy pathCandyLeetcode.java
File metadata and controls
28 lines (23 loc) · 700 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
importjava.util.Arrays;
publicclassCandyLeetcode {
publicintcandy(int[] ratings) {
intn = ratings.length;
int[] candies = newint[n];
Arrays.fill(candies, 1);
for (inti = 1; i < n; i++) {
if (ratings[i] > ratings[i - 1]) {
candies[i] = candies[i - 1] + 1;
}
}
for (inti = n - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
}
}
inttotalCandies = 0;
for (inti = 0; i < n; i++) {
totalCandies += candies[i];
}
returntotalCandies;
}
}