|
3 | 3 | importjava.util.Arrays;
|
4 | 4 |
|
5 | 5 | /**
|
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. |
7 | 14 |
|
8 | 15 | Note:
|
9 | 16 | You may assume the greed factor is always positive.
|
10 | 17 | You cannot assign more than one cookie to one child.
|
11 | 18 |
|
12 | 19 | Example 1:
|
13 | 20 | Input: [1,2,3], [1,1]
|
14 |
| -
|
15 | 21 | Output: 1
|
16 |
| -
|
17 | 22 | Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
|
18 | 23 | 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.
|
19 | 24 | You need to output 1.
|
| 25 | +
|
20 | 26 | Example 2:
|
21 | 27 | Input: [1,2], [1,2,3]
|
22 |
| -
|
23 | 28 | Output: 2
|
24 |
| -
|
25 | 29 | Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
|
26 | 30 | You have 3 cookies and their sizes are big enough to gratify all of the children,
|
27 | 31 | You need to output 2.*/
|
28 | 32 |
|
29 | 33 | publicclass_455 {
|
30 | 34 |
|
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++; |
40 | 47 | }
|
41 |
| -j++; |
| 48 | +returnresult; |
42 | 49 | }
|
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)); |
51 | 50 | }
|
52 | 51 |
|
53 | 52 | }
|