AlgoMaster Logo

Assign Cookies

Last Updated: March 29, 2026

easy

Understanding the Problem

We have two lists: children (each with a minimum cookie size they'll accept) and cookies (each with a size). We need to match cookies to children so that the maximum number of children are satisfied. Each child gets at most one cookie, and each cookie goes to at most one child.

This is essentially a matching problem. We want to pair up children and cookies such that each paired cookie is large enough for its child, and we maximize the total number of pairings.

The key observation is that we should avoid wasting large cookies on children who would be happy with smaller ones. If a child only needs a cookie of size 1, giving them a cookie of size 10 wastes a resource that could satisfy a pickier child. This naturally leads to a greedy strategy: sort both lists and try to satisfy the least greedy children first, using the smallest cookies that work.

Key Constraints:

  • 1 <= g.length <= 3 * 10^4 → With n up to 30,000, O(n log n) sorting is fine. O(n^2) brute force is borderline (up to 900 million operations) and likely too slow.
  • 0 <= s.length <= 3 * 10^4 → The cookie array can be empty, which means the answer is 0. We need to handle this edge case.
  • 1 <= g[i], s[j] <= 2^31 - 1 → Values can be very large, but since we only compare sizes (no arithmetic), overflow isn't a concern.

Approach 1: Brute Force

Intuition

The most natural first attempt: for each child, scan through all the cookies to find one that's big enough. Once a cookie is used, mark it as taken so no other child gets it. To maximize satisfied children, we should try to satisfy the least greedy children first (they're easier to please), so we sort the children array first.

This works correctly, but for each child we potentially scan through all cookies, giving us O(n * m) time where n is the number of children and m is the number of cookies.

Algorithm

  1. Sort the greed factor array g in ascending order.
  2. Create a boolean array used of size m (number of cookies), initialized to all false.
  3. Initialize contentChildren = 0.
  4. For each child (in sorted order of greed):
    • Scan through all cookies to find the smallest unused cookie that satisfies this child.
    • If found, mark that cookie as used and increment contentChildren.
  5. Return contentChildren.

Example Walkthrough

1Sorted g=[1,2,3], s=[1,1]. Start with child 0 (greed=1)
0
1
child i=0
1
2
2
3
1/5
1Cookies sorted: [1, 1]. All unused.
0
1
1
1
1/5

Code

Since both arrays are sorted, once we pass a cookie that's too small for the current child, it's too small for all remaining children too. What if we just marched forward through both arrays simultaneously, never going back?

Approach 2: Greedy with Two Pointers (Optimal)

Intuition

Here's the core insight: if both arrays are sorted, we can walk through them in lockstep. Start with the least greedy child and the smallest cookie. If the smallest cookie satisfies the least greedy child, great, assign it and move on to the next child and next cookie. If the cookie is too small, skip it (it won't satisfy any remaining child either) and try the next cookie.

Why is this greedy strategy optimal? If a small cookie can satisfy a child, there's no benefit in saving that small cookie for a greedier child (who needs a bigger cookie anyway). And there's no benefit in using a larger cookie for a less greedy child when a smaller one would do, because that larger cookie might be the only one that satisfies a greedier child later. So by sorting both and matching smallest-to-smallest, we never waste resources.

Algorithm

  1. Sort both g (greed factors) and s (cookie sizes) in ascending order.
  2. Initialize two pointers: childIndex = 0 (points to the current child) and cookieIndex = 0 (points to the current cookie).
  3. While both pointers are within bounds:
    • If s[cookieIndex] >= g[childIndex], the cookie satisfies the child. Move both pointers forward.
    • Otherwise, the cookie is too small. Move only the cookie pointer forward.
  4. Return childIndex (the number of children satisfied).

Example Walkthrough

1Sorted: g=[1,2,3], s=[1,1]. childIndex=0, cookieIndex=0
0
1
childIndex
1
2
2
3
1/4
1Cookies sorted: [1, 1]. cookieIndex starts at 0.
0
1
cookieIndex
1
1
1/4

Code