Skip to content

Commit 5c7bfd6

Browse files
committed
new soln
1 parent 6df8411 commit 5c7bfd6

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

455.FindContentChildren.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// 455. Assign Cookies
2+
// Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
3+
// Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], 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.
4+
// Example 1:
5+
// Input: g = [1,2,3], s = [1,1]
6+
// Output: 1
7+
// Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
8+
// 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.
9+
// You need to output 1.
10+
// Example 2:
11+
// Input: g = [1,2], s = [1,2,3]
12+
// Output: 2
13+
// Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
14+
// You have 3 cookies and their sizes are big enough to gratify all of the children,
15+
// You need to output 2.
16+
// Constraints:
17+
// 1 <= g.length <= 3 * 104
18+
// 0 <= s.length <= 3 * 104
19+
// 1 <= g[i], s[j] <= 231 - 1
20+
21+
public class Solution {
22+
public int FindContentChildren(int[] g, int[] s) {
23+
int count = 0;
24+
int i = 0, j = 0;
25+
Array.Sort(g);
26+
Array.Sort(s);
27+
while(i < g.Length && j < s.Length){
28+
if(g[i] <= s[j]){
29+
i++;
30+
count++;
31+
}
32+
j++;
33+
}
34+
return count;
35+
}
36+
}

0 commit comments

Comments
 (0)