A group of children are handed out cookies. Child i has a greed factor greed[i], and a child becomes content only when handed a single cookie whose size is at least that child's greed factor. There are some cookies available, cookie j having size sizes[j]. Each cookie can be given to at most one child, and each child may receive at most one cookie.
Distribute the cookies so that the number of content children is as large as possible, and print that maximum number.
Line 1: an integer g, the number of children.
Line 2: g space-separated integers, the greed factors (this line is empty when g = 0).
Line 3: an integer s, the number of cookies.
Line 4: s space-separated integers, the cookie sizes (this line is empty when s = 0).
A single integer: the maximum number of children that can be made content.
Example 1
Input
2 1 3 3 1 2 3
Expected
2
Explanation
Sorted greeds are [1, 3] and sorted cookie sizes are [1, 2, 3]. Cookie of size 1 satisfies the child with greed 1, and cookie of size 3 satisfies the child with greed 3. Both children become content, so the answer is 2.
Example 2
Input
3 5 10 15 2 4 8
Expected
1
Explanation
Sorted greeds are [5, 10, 15] and cookies are [4, 8]. The size-4 cookie cannot satisfy the smallest greed (5), but the size-8 cookie can. Only one child can be made content, so the answer is 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →