-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCandy (Manna)
More file actions
32 lines (28 loc) · 823 Bytes
/
Candy (Manna)
File metadata and controls
32 lines (28 loc) · 823 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
29
30
31
32
Enter file contents here
public class Solution {
public int candy(int[] ratings) {
int [] candy = new int[ratings.length];
int count = 0;
candy[0] = 1;
int left = ratings[0];
for (int i = 1; i < ratings.length; i++) {
if (ratings[i] > left) {
candy[i] = candy[i - 1] + 1;
} else {
candy[i] = 1;
}
left = ratings[i];
}
int right = ratings[ratings.length - 1];
for (int j = ratings.length - 2; j >= 0; j--) {
if (ratings[j] > right&& candy[j] <= candy[j+1]) {
candy[j] = candy[j + 1] + 1;
}
right = ratings[j];
}
for (int i = 0; i < candy.length; i++) {
count += candy[i];
}
return count;
}
}