-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignCookies.java
More file actions
53 lines (47 loc) · 1.04 KB
/
AssignCookies.java
File metadata and controls
53 lines (47 loc) · 1.04 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
53
import java.util.Arrays;
public class AssignCookies {
public static void main(String[] args) {
int[][] greeds = {
{1,5,3,3,4},
{2,3,5},
{2,2},
{1,2,3},
{},
{1,2},
{2,2,2},
{1000000,2},
{0,0}
};
int[][] cookies = {
{4,2,1,2,1,3},
{1,1},
{3,3,3},
{1,2,3},
{1,2},
{},
{2,2},
{1000000},
{0}
};
int[] expected = {3, 0, 2, 3, 0, 0, 2, 1, 1};
for (int i = 0; i < greeds.length; i++) {
int sat = getMaximumSatisfiedChildren(greeds[i], cookies[i]);
System.out.printf("Case %d: greed=%s cookie=%s -> got=%d expected=%d%n",
i+1, Arrays.toString(greeds[i]), Arrays.toString(cookies[i]), sat, expected[i]);
}
}
public static int getMaximumSatisfiedChildren(int[] greed, int[] cookie) {
Arrays.sort(greed);
Arrays.sort(cookie);
int satisfied = 0;
int greedIdx = 0;
for (int c : cookie) {
if (greedIdx>=greed.length) break;
if (greed[greedIdx]<=c) {
satisfied++;
greedIdx++;
}
}
return satisfied;
}
}