Skip to content
BytePatterns

Hand Out Cookies

EasyGreedy#greedy#sorting#two-pointers~15m

Problem

Each child has a smallest cookie size that will make them happy, and each cookie has a size. A child gets at most one cookie and a cookie goes to at most one child. Return the largest number of children you can make happy.

Examples

Input:  wants = [1, 2, 3], sizes = [1, 1]
Output: 1
Why:    both cookies only satisfy the child who wants size 1
Input:  wants = [1, 2], sizes = [1, 2, 3]
Output: 2
Input:  wants = [5], sizes = []
Output: 0
Why:    edge case, no cookies means no happy children

Hints

0 / 3

Stuck on the idea rather than the code? What Makes Greedy Work covers it.