2Brute2Best
πŸ”₯0

Assign Cookies

EasyΒ·LeetCode #455Basic Anchor
LeetCode
30-Second Mental ModelResource assignment: sort both, match the smallest that works
Complexity:O((M + 1)^N)β†’O(N log N + M log M)
Key Insight

Sort the children by greed and the cookies by size. Walk the cookies from smallest to biggest, with a pointer to the least greedy child who has no cookie yet. If the cookie is big enough for that child, give it to them and move the pointer on. If it is too small, skip it. The pointer's final position is the answer.

Core Validity Invariant

A cookie can make the waiting child happy only if the cookie is at least as big as that child's greed: s[cookie] β‰₯ g[child].

01

Problem

You are handing out cookies to some children. Each child has a greed number: the smallest cookie size that makes them happy. Each cookie has a size.

A child is happy only with a cookie that is at least as big as their greed number. Each child can get at most one cookie, and each cookie can go to at most one child.

Hand out the cookies so that as many children as possible are happy. Return that number.

Input

g, the greed numbers of the children, and s, the sizes of the cookies. s can be empty.

Output

One integer: the largest number of children that can be happy.

Example 1
Input: g = [1, 2, 3], s = [1, 1]
Output: 1

Both cookies are size 1, so only the child with greed 1 can be happy. The other two need a cookie of size 2 or more.

Example 2
Input: g = [1, 2], s = [1, 2, 3]
Output: 2

Give the greed-1 child a cookie of size 1, and the greed-2 child a cookie of size 2. Both are happy, and the size-3 cookie is left over.

Constraints
  • β€’ 1 ≀ g.length ≀ 3 Β· 10^4
  • β€’ 0 ≀ s.length ≀ 3 Β· 10^4
  • β€’ 1 ≀ g[i], s[j] ≀ 2^31 βˆ’ 1
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck β€” each hint is counted.

  1. 1.

    What is the goal: to make the most children happy, or to use up the most cookies?

  2. 2.

    If I give a big cookie to a child who only needed a small one, what might I be blocking for a greedier child?

  3. 3.

    If a cookie is too small for the least greedy child, can it still help anyone?

  4. 4.

    Does the order of the children matter here? Does the order of the cookies matter?

  5. 5.

    For g = [1, 2, 3] and s = [1, 1], is the answer the number of cookies? Why or why not?

03

Brute force approach

04

Better approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceBacktrack over the children: each one gets no cookie, or any free cookie that is big enoughO((M + 1)^N)O(N + M)
BetterTake the least greedy child first, and scan all cookies for the smallest free one that fitsO(N Β· M)O(M)
OptimalSort both lists. Walk the cookies from smallest, and give each one to the least greedy waiting child if it fitsO(N log N + M log M)O(1)
09

Why does this belong to this pattern?

Clues in the problem statement
  • β€œeach child gets at most one cookie”Each child and each cookie can be used at most once, so this is a one-to-one pairing.
  • β€œgreed factor”A minimum: a cookie works only if its size is at least the greed.
  • β€œmaximize the number of content children”Count the pairs, and use the small cookies on the children who can take them.
Pair each child with a cookie of size β‰₯ greed+each item used at most once+maximise the number of pairsβ†’Resource assignment: sort both, match the smallest that works

This is the anchor of the assignment sub-pattern. The rule is give the smallest cookie that is big enough, so the big cookies stay free for greedy children. The leftover cookies are simply not counted. Advantage Shuffle is different: every value there must be used, so a losing pair still has to be paired with something.

10

Don't make this mistake

Pattern confusion: giving the least greedy child the biggest cookie
βœ— for each child (least greedy first): give the BIGGEST cookie that is big enough
βœ“ for each child (least greedy first): give the SMALLEST cookie that is big enough

The big cookies are needed by the greedy children. On g = [3, 1, 5, 2] and s = [4, 1, 1], the wrong rule gives the greed-1 child the 4. After that, the greed-2 child can only use a 1, so nobody else is happy. Result: 1. The right rule gives the greed-1 child a 1 and the greed-2 child the 4. Result: 2.

Using > instead of β‰₯
βœ— if (s[j] > g[i])
βœ“ if (s[j] >= g[i])

A cookie that is exactly the child's greed makes them happy. With g = [1] and s = [1], the > check says 0 children are happy, but the answer is 1.

Returning the smaller list length
βœ— return min(g.size(), s.size());
βœ“ return the child pointer after the walk

Counting cookies is not the same as counting happy children. With g = [1, 2, 3] and s = [1, 1], the min is 2. But the second cookie is too small for the greed-2 child, so the answer is 1.

Sorting only one list
βœ— sort(g.begin(), g.end()); // s is still in the given order
βœ“ sort(g.begin(), g.end()); sort(s.begin(), s.end());

The walk needs both lists in order. With g = [1, 2] and s = [3, 1], the cookie 3 comes first and makes the greed-1 child happy. Then the cookie 1 is too small for the greed-2 child. Result: 1. With both sorted, the cookie 1 goes to the greed-1 child and the cookie 3 to the greed-2 child. Result: 2.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.