AlgoMaster Logo

Assign Cookies

easyFrequency5 min readUpdated June 23, 2026

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 a matching problem: pair children with cookies so that each paired cookie is at least as large as its child's greed factor, and maximize the number of pairs.

A large cookie spent on a child who would accept a smaller one is wasted, because that cookie might be the only one big enough for a pickier child. Avoiding this waste suggests a greedy strategy: sort both lists, satisfy the least greedy children first, and give each child the smallest cookie that works.

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; the answer is then 0.
  • 1 <= g[i], s[j] <= 2^31 - 1 → Values can reach the maximum 32-bit integer, but since we only compare sizes (no arithmetic), overflow isn't a concern.

Approach 1: Brute Force

Intuition

For each child, scan the cookies and take the first unused one that is big enough. Sort both arrays first: children are then processed from least to most greedy, and the first unused cookie that fits is also the smallest one that fits, so no large cookie is spent where a small one would do.

Each child may scan the entire cookie array, so the matching step costs O(n * m) on top of the sorting, where n is the number of children and m is the number of cookies.

Algorithm

  1. Sort both g (greed factors) and s (cookie sizes) 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 the cookies from smallest to largest and take the first unused cookie with s[j] >= g[i].
    • If one exists, mark it as used and increment contentChildren.
  5. Return contentChildren.

Example Walkthrough

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

Code

The sorting already does most of the work here. Once a cookie fails the current child, it fails every remaining child too, since the children are sorted by greed. A single forward pass over both arrays, never revisiting a cookie, replaces the nested scan.

Approach 2: Greedy with Two Pointers (Optimal)

Intuition

With both arrays sorted, walk through them in lockstep, starting with the least greedy child and the smallest cookie. If the current cookie satisfies the current child, assign it and advance both pointers. If it is too small, advance only the cookie pointer: a cookie that cannot satisfy the least greedy remaining child cannot satisfy anyone else, so it can be discarded permanently.

Every cookie and every child is visited at most once, so the pass after sorting is linear in n + m.

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

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

Code