|
| 1 | +# Assign Cookies |
| 2 | + |
| 3 | +Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most |
| 4 | +one cookie. |
| 5 | + |
| 6 | +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 |
| 7 | +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. |
| 8 | +Your goal is to maximize the number of your content children and output the maximum number. |
| 9 | + |
| 10 | +## Constraints |
| 11 | + |
| 12 | +- 1 <= greed.length <= 3 * 104 |
| 13 | +- 0 <= cookies.length <= 3 * 104 |
| 14 | +- 1 <= greed[i], cookies[j] <= 231 - 1 |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | +Example 1: |
| 19 | +```text |
| 20 | +Input: g = [1,2,3], s = [1,1] |
| 21 | +Output: 1 |
| 22 | +Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. |
| 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 |
| 24 | +content. |
| 25 | +You need to output 1. |
| 26 | +``` |
| 27 | + |
| 28 | +Example 2: |
| 29 | +```text |
| 30 | +Input: g = [1,2], s = [1,2,3] |
| 31 | +Output: 2 |
| 32 | +Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. |
| 33 | +You have 3 cookies and their sizes are big enough to gratify all of the children, |
| 34 | +You need to output 2. |
| 35 | +``` |
| 36 | + |
| 37 | +## Topics |
| 38 | + |
| 39 | +- Array |
| 40 | +- Two Pointers |
| 41 | +- Greedy |
| 42 | +- Sorting |
| 43 | + |
| 44 | +## Solution |
| 45 | + |
| 46 | +Intiutively, we want to give each child the smallest cookie that satisfies them. This allows us to save the larger |
| 47 | +cookies for the greedier children and allows us to maximize the number of satisfied children. |
| 48 | + |
| 49 | +The greedy algorithm sorts both the greeds and cookies arrays in ascending order. This places the child with the smallest |
| 50 | +greed and the smallest cookie at the front of each array. |
| 51 | + |
| 52 | +For example: |
| 53 | + |
| 54 | +```text |
| 55 | +greeds = [1, 3, 3, 4] |
| 56 | +cookies = [2, 2, 3, 4] |
| 57 | +``` |
| 58 | + |
| 59 | +We then initialize two pointers `i` and `j` to the start of the `greeds` and `cookies` arrays, respectively. `i` |
| 60 | +represents the current child and `j` represents the current cookie. |
| 61 | + |
| 62 | +If `cookies[j] >= greeds[i]`, that means the current cookie can satisfy the current child. We increment the number of |
| 63 | +satisfied children and move to the next child and cookie. |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +If `cookies[j] < greeds[i]`, that means the current cookie cannot satisfy the current child, so we move to the next cookie |
| 68 | +to see if it can. |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +We can continue this process until we reach the end of either the greeds or cookies arrays, and return the number of |
| 73 | +satisfied children as the result. |
| 74 | + |
| 75 | +### Complexity Analysis |
| 76 | + |
| 77 | +#### Time Complexity |
| 78 | + |
| 79 | +O(n log n + m log m) where n is the number of children and m is the number of cookies. We sort the |
| 80 | +greeds and cookies arrays in O(n log n + m log m) time, and then iterate through the arrays in O(n + m) time. |
| 81 | + |
| 82 | +#### Space Complexity |
| 83 | + |
| 84 | +O(1) We only use a constant amount of space for variables. |
0 commit comments