oneby7

Greedy The best choice now

499. Assign Cookies

Easy

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]

Output: 3

Explanation: 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]

Output: 2

Explanation: 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 = []

Output: 0

Explanation: 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.

greed = [8, 5, 6, 5], cookies = [1, 6, 5, 5]

Constraints

  • 1 ≤ greed.length ≤ 105
  • 0 ≤ cookies.length ≤ 105
  • 1 ≤ greed[i], cookies[j] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.