Assign Cookies
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.
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].
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.
g, the greed numbers of the children, and s, the sizes of the cookies. s can be empty.
One integer: the largest number of children that can be happy.
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.
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.
- β’ 1 β€ g.length β€ 3 Β· 10^4
- β’ 0 β€ s.length β€ 3 Β· 10^4
- β’ 1 β€ g[i], s[j] β€ 2^31 β 1
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck β each hint is counted.
- 1.
What is the goal: to make the most children happy, or to use up the most cookies?
- 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.
If a cookie is too small for the least greedy child, can it still help anyone?
- 4.
Does the order of the children matter here? Does the order of the cookies matter?
- 5.
For g = [1, 2, 3] and s = [1, 1], is the answer the number of cookies? Why or why not?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Backtrack over the children: each one gets no cookie, or any free cookie that is big enough | O((M + 1)^N) | O(N + M) |
| Better | Take the least greedy child first, and scan all cookies for the smallest free one that fits | O(N Β· M) | O(M) |
| Optimal | Sort both lists. Walk the cookies from smallest, and give each one to the least greedy waiting child if it fits | O(N log N + M log M) | O(1) |
Why does this belong to this pattern?
- β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.
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.
Don't make this mistake
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.
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.
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.
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.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.