Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8b914d5

Browse files
refactor 455
1 parent7fdadb1 commit8b914d5

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
importjava.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
publicclass_455 {
3034

31-
publicintfindContentChildren(int[]g,int[]s) {
32-
Arrays.sort(g);
33-
Arrays.sort(s);
34-
35-
intresult =0;
36-
for (inti =0,j =0;i <g.length &&j <s.length; ) {
37-
if (s[j] >=g[i]) {
38-
result++;
39-
i++;
35+
publicstaticclassSolution1 {
36+
publicintfindContentChildren(int[]g,int[]s) {
37+
Arrays.sort(g);
38+
Arrays.sort(s);
39+
40+
intresult =0;
41+
for (inti =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+
returnresult;
4249
}
43-
returnresult;
44-
}
45-
46-
publicstaticvoidmain(String...args) {
47-
_455test =new_455();
48-
int[]g =newint[]{1,2,3};
49-
int[]s =newint[]{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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._455;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticjunit.framework.Assert.assertEquals;
8+
9+
publicclass_455Test {
10+
privatestatic_455.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_455.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(1,solution1.findContentChildren(newint[]{1,2,3},newint[]{1,1}));
20+
21+
}
22+
23+
@Test
24+
publicvoidtest2() {
25+
assertEquals(2,solution1.findContentChildren(newint[]{1,2},newint[]{1,2,3}));
26+
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp