-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCandy.java
More file actions
28 lines (25 loc) · 731 Bytes
/
Candy.java
File metadata and controls
28 lines (25 loc) · 731 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
// Exclusive Intuitive Slope method!
class Solution {
public int candy(int[] ratings) {
int n = ratings.length;
if (n <= 1) return n;
int candies = 1;
int up = 0, down = 0, peak = 0;
for (int i = 1; i < n; i++) {
if (ratings[i] > ratings[i - 1]) {
up++;
peak = up;
down = 0;
candies += 1 + up;
} else if (ratings[i] == ratings[i - 1]) {
up = down = peak = 0;
candies += 1;
} else {
up = 0;
down++;
candies += 1 + down - (peak >= down ? 1 : 0);
}
}
return candies;
}
}