All problems

Assign Cookies

Easy · greedy

A teacher has a bag of cookies to share out. Child i will only be content with a cookie of size at least greed[i]. Cookie j has size cookies[j].

Each child can receive at most one cookie, and each cookie can go to at most one child. Return the largest number of children that can end up content.

Both lists are given in non-decreasing order (smallest first). If no child can be satisfied, return 0.

Examples

Example 1

Input: greed = [1, 2, 3], cookies = [1, 1]

Output: 1

Only the child with greed 1 can be satisfied, by either cookie.

Example 2

Input: greed = [2, 3, 5], cookies = [1, 2, 4]

Output: 2

Give the 2-cookie to the child with greed 2 and the 4-cookie to the child with greed 3. Nobody can use the 1-cookie.

Example 3

Input: greed = [1, 4], cookies = [1, 2, 3, 4, 5]

Output: 2

Constraints