-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandy135.java
More file actions
26 lines (23 loc) · 791 Bytes
/
candy135.java
File metadata and controls
26 lines (23 loc) · 791 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
public class candy135 {
public static void main(String[] args){
int[] ratings = new int[]{1,3,2,2,1}; //1,2,2 1,0,2 1,3,4,5,2
int result = candy(ratings);
System.out.println(result);
}
public static int candy(int[] ratings) {
int n = ratings.length;
int sum = 0;
int[] sugar = new int[n];
for(int i = 0; i < n; i++) sugar[i] = 1;
for(int i =0; i < n -1; i++) {
if( ratings[i] < ratings[i+1]) sugar[i+1] =sugar[i] +1;
}
for(int j = n-1; j > 0; j--) {
if(ratings[j] < ratings[j -1] && sugar[j] >= sugar[j-1]) sugar[j-1] = sugar[j] +1;
}
for(int i = 0; i < n; i++) {
sum += sugar[i];
}
return sum;
}
}