Greedy The best choice now
499. Assign Cookies
You have some children and some cookies. greed[i] is the greed of the i-th child: the smallest cookie
that will make that child content. cookies[j] is the size of the j-th cookie.
A child is content when they get a cookie whose size is at least their greed. Each child gets at most one cookie, and each cookie goes to at most one child. Some children may get nothing, and some cookies may be left over.
Return the largest number of children you can make content.
Example 1
Input: greed = [5, 10, 2, 7], cookies = [6, 1, 8, 3, 4]
3Explanation: The child of greed 2 gets the cookie of size 3, the child of greed 5 gets 6, and the child of greed 7 gets 8. No cookie is as big as 10, so the fourth child cannot be content.
Example 2
Input: greed = [6, 6, 6], cookies = [6, 9]
2Explanation: A cookie of size exactly 6 is enough for a child of greed 6. There are only two cookies, so one child gets nothing.
Example 3
Input: greed = [2, 1], cookies = []
0Explanation: There are no cookies, so no child can be content.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ greed.length ≤ 1050 ≤ cookies.length ≤ 1051 ≤ greed[i], cookies[j] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.