Menu
CoddyTech

Assign Cookies

Each child i has a greed factor g[i]: the smallest cookie size that makes them happy. Each cookie j has a size s[j]. A child is content when they get one cookie whose size is at least their greed factor. Every child gets at most one cookie and every cookie goes to at most one child. Return the largest number of children you can make content.

Function

findContentChildren(g: integer-array, s: integer-array) → integer
ginteger-array
the greed factor of each child, the smallest cookie size they accept
sinteger-array
the size of each cookie
Returnsinteger
the most children that can each get a cookie at least as big as their greed factor

Constraints

  • 1 ≤ g.length, s.length ≤ 5000
  • 1 ≤ g[i], s[j] ≤ 105
  • The two arrays may have different lengths, and neither is sorted.

Examples

Input
g = [4, 2, 7]s = [3, 5, 1, 2]
Output
2
Explanation
Sorted, the children want 2, 4 and 7 and the cookies are 1, 2, 3 and 5. Cookie 2 feeds the child who wants 2 and cookie 5 feeds the child who wants 4. Nothing is left that reaches 7, so the answer is 2.

lock icon+16 hidden tests on Submit

challenge icon

Follow-up

What if each child also has a largest cookie they will accept, so a cookie fits only inside a range? Which waiting child should each cookie go to then?

Reset code
def findContentChildren(g, s):
    # Write code here
Test cases

Case 1

Case 2

Input

g = [4, 2, 7]
s = [3, 5, 1, 2]

Expected

2