-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCandy.java
More file actions
52 lines (44 loc) · 1.34 KB
/
Candy.java
File metadata and controls
52 lines (44 loc) · 1.34 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
*/
import java.util.*;
public class Candy {
public static int candy(int[] ratings) {
if(ratings.length <= 0) return 0;
int s = ratings.length;
if(s == 1) return 1;
int[] candy = new int[s];
candy[0] = 1;
for(int i = 1; i < s; i++){
if(ratings[i] > ratings[i-1]){
candy[i] = candy[i-1] + 1;
}else{
candy[i] = 1;
}
}
for(int i = s-2; i>= 0; i--){
if(ratings[i] > ratings[i+1] && candy[i] <= candy[i+1]){
candy[i] = candy[i+1]+1;
}
}
int res = 0;
for(int i = 0; i < s; i++){
res += candy[i];
}
return res;
}
public static void main(String[] args) {
int[] nums1 = new int[] {1, 4, 3, 3, 2, 5};
int res = candy(nums1);
for(int i=0; i < nums1.length; i++){
System.out.print(nums1[i] + " ");
}
System.out.println();
System.out.println("Minimum candy: " + res);
return;
}
}