Skip to content

Hand Out Cookies

Easy#greedy#sorting#two-pointers~15 min

The mission

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

Target time
O(n log n + m log m)
Target space
O(n + m)

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

Python 3Ctrl+Enter runs
Custom test case