Skip to content

Commit 2261e86

Browse files
committed
added Assign Cookies (easy)
1 parent 93dee59 commit 2261e86

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

Easy/AssignCookies/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# Assign Cookies
3+
[Leetcode Link](https://leetcode.com/problems/assign-cookies/)
4+
5+
## Problem:
6+
7+
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.
8+
9+
## Example:
10+
11+
```
12+
Input: [1,2,3], [1,1]
13+
14+
Output: 1
15+
16+
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3.
17+
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.
18+
You need to output 1.
19+
```
20+
```
21+
Input: [1,2], [1,2,3]
22+
23+
Output: 2
24+
25+
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
26+
You have 3 cookies and their sizes are big enough to gratify all of the children,
27+
You need to output 2.
28+
```
29+
30+
## Note:
31+
32+
- You may assume the greed factor is always positive.
33+
- You cannot assign more than one cookie to one child.

Easy/AssignCookies/Solution.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package Easy.AssignCookies;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Solution
7+
*/
8+
public class Solution {
9+
10+
// test driver
11+
public static void main(String[] args) {
12+
Solution sol = new Solution();
13+
int[] greeds = { 1, 2, 2, 1, 3, 5 };
14+
int[] sizes = { 1, 2, 2, 2, 2 };
15+
System.out.println(sol.findContentChildren(greeds, sizes));
16+
}
17+
18+
public int findContentChildren(int[] g, int[] s) {
19+
int fed = 0;
20+
// sort both greed factors and cookie sizes
21+
Arrays.sort(g);
22+
Arrays.sort(s);
23+
System.out.println(Arrays.toString(g));
24+
System.out.println(Arrays.toString(s));
25+
int i = 0;
26+
int j = 0;
27+
while (i < g.length && j < s.length) {
28+
if (g[i] <= s[j]) {
29+
// feed the cookie
30+
fed++;
31+
i++;
32+
}
33+
j++;
34+
}
35+
return fed;
36+
}
37+
38+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Languages used: Java and Python
4040
- [Remove Duplicates from Sorted List](Easy/RemoveDuplicatesFromSortedList)
4141
- [Longest Continuous Increasing Subsequence](Easy/LongestIncreasingSubsequence)
4242
- [Happy Number](Easy/HappyNumber)
43+
- [Assign Cookies](Easy/AssignCookies)
4344
- Medium
4445
- [Minimum Add to Make Parentheses Valid](Medium/MinimumAddtoMakeParenthesesValid)
4546
- [Distribute Coins in Binary Tree](Medium/DistributionCoinsInBinaryTree)

0 commit comments

Comments
 (0)