Editor

Cookies for Children

#58 · Greedy · Intermediate · 60 XP · about 10 min

Each child has a greed level: greed[i] is the smallest cookie size that makes child i happy. Each cookie has a size, sizes[j]. A child gets at most one cookie, and a cookie goes to at most one child.

Write content_children(greed, sizes) that returns the largest number of children you can make happy.

Examples

content_children([3, 1, 2], [2, 1])     # returns 2
content_children([1, 2], [3, 2, 1, 1])  # returns 2
content_children([5, 5], [1, 2, 3])     # returns 0

In the first example the child with greed 1 gets the size 1 cookie and the child with greed 2 gets the size 2 cookie; the child with greed 3 goes without.

Constraints

  • 0 <= len(greed), len(sizes) <= 30000
  • 1 <= greed[i], sizes[j] <= 10**9

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.