Skip to content

Commit 6190943

Browse files
refactor 455
1 parent 1cfd56c commit 6190943

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

src/main/java/com/fishercoder/solutions/_455.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,50 @@
33
import java.util.Arrays;
44

55
/**
6-
* Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
6+
* 455. Assign Cookies
7+
*
8+
* Assume you are an awesome parent and want to give your children some cookies.
9+
* But, you should give each child at most one cookie.
10+
* Each child i has a greed factor gi, which is the minimum size of a cookie
11+
* that the child will be content with;
12+
* and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i,
13+
* and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
714
815
Note:
916
You may assume the greed factor is always positive.
1017
You cannot assign more than one cookie to one child.
1118
1219
Example 1:
1320
Input: [1,2,3], [1,1]
14-
1521
Output: 1
16-
1722
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
1823
And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content.
1924
You need to output 1.
25+
2026
Example 2:
2127
Input: [1,2], [1,2,3]
22-
2328
Output: 2
24-
2529
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
2630
You have 3 cookies and their sizes are big enough to gratify all of the children,
2731
You need to output 2.*/
2832

2933
public class _455 {
3034

31-
public int findContentChildren(int[] g, int[] s) {
32-
Arrays.sort(g);
33-
Arrays.sort(s);
34-
35-
int result = 0;
36-
for (int i = 0, j = 0; i < g.length && j < s.length; ) {
37-
if (s[j] >= g[i]) {
38-
result++;
39-
i++;
35+
public static class Solution1 {
36+
public int findContentChildren(int[] g, int[] s) {
37+
Arrays.sort(g);
38+
Arrays.sort(s);
39+
40+
int result = 0;
41+
for (int i = 0, j = 0; i < g.length && j < s.length; ) {
42+
if (s[j] >= g[i]) {
43+
result++;
44+
i++;
45+
}
46+
j++;
4047
}
41-
j++;
48+
return result;
4249
}
43-
return result;
44-
}
45-
46-
public static void main(String... args) {
47-
_455 test = new _455();
48-
int[] g = new int[]{1, 2, 3};
49-
int[] s = new int[]{1, 1};
50-
System.out.println(test.findContentChildren(g, s));
5150
}
5251

5352
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._455;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class _455Test {
10+
private static _455.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _455.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.findContentChildren(new int[]{1, 2, 3}, new int[]{1, 1}));
20+
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(2, solution1.findContentChildren(new int[]{1, 2}, new int[]{1, 2, 3}));
26+
27+
}
28+
}

0 commit comments

Comments
 (0)