Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie.
Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number.
Example 1:
Input: g = [1,2,3], s = [1,1] Output: 1 Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are 1, 2, 3. And even though you have 2 cookies, since their size is both 1, you could only make the child whose greed factor is 1 content. You need to output 1.
Example 2:
Input: g = [1,2], s = [1,2,3] Output: 2 Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2. You have 3 cookies and their sizes are big enough to gratify all of the children, You need to output 2.
Constraints:
1 <= g.length <= 3 * 1040 <= s.length <= 3 * 1041 <= g[i], s[j] <= 231 - 1
Note: This question is the same as 2410: Maximum Matching of Players With Trainers.
Problem Overview: Each child has a greed factor g[i] and each cookie has a size s[j]. A child is satisfied only if the cookie size is greater than or equal to their greed factor. Each cookie can be assigned to only one child. The task is to maximize the number of satisfied children.
Approach 1: Greedy with Sorting + Two Pointers (O(n log n + m log m) time, O(1) space)
The key observation: smaller cookies should go to children with the smallest greed first. If you give a large cookie to a less greedy child early, you may waste it and fail to satisfy a greedier child later. Sort both arrays, then walk through them using two pointers. One pointer tracks children, the other tracks cookies. If the current cookie satisfies the child (s[j] >= g[i]), assign it and move both pointers. Otherwise, move the cookie pointer to try a larger cookie. Sorting ensures you always attempt the smallest feasible assignment first.
This approach relies on a classic greedy strategy combined with sorting. Each cookie and child is processed once after sorting, so the pointer traversal is linear. The sorting step dominates the runtime.
Approach 2: Greedy with Sorting + Binary Search (O(n log n + m log m + n log m) time, O(1) space)
Another strategy is to still sort both arrays but locate cookies using binary search. For each child (processed from smallest greed to largest), perform a binary search on the cookie array to find the smallest unused cookie that satisfies the greed factor. Once found, mark it as used and move to the next child. This works because sorting keeps the cookies ordered, allowing efficient lookup.
This version replaces the linear two‑pointer scan with repeated binary searches. While conceptually straightforward, it is slightly slower because each child may trigger a log m search. The method still follows the same greedy rule: assign the smallest valid cookie so larger ones remain available for greedier children. It demonstrates how array ordering enables efficient selection strategies.
Recommended for interviews: The sorting + two pointers greedy solution is the expected answer. It is simple, optimal, and demonstrates clear greedy reasoning. Interviewers usually want to see the insight that matching the smallest cookie with the least greedy child avoids wasting resources. The binary search variant works but adds unnecessary overhead compared to the clean linear scan.
This approach involves sorting the greed factors and cookie sizes. By sorting, the goal is to try to satisfy the least greedy child first with the smallest satisfying cookie. By continuing this way, the solution can maximize the number of content children.
The solution uses two pointers, one for the greed list (g) and one for the cookie size list (s). It increments the pointer for the children only when a matching cookie is found.
This C solution sorts both arrays and uses a two-pointer technique. It keeps a pointer on both the greed factors array and the cookie sizes array. By incrementing both pointers when a child is satisfied, it ensures that all possible cookies are used optimally.
Time Complexity: O(n log n + m log m), where n is the number of children and m is the number of cookies (due to sorting).
Space Complexity: O(1), as it uses a constant amount of extra space apart from the input arrays.
This involves using binary search to attempt to find the proper index in the sorted list `s` where a given greed factor `g[i]` meets the condition.
For each child in the sorted greed list, perform binary search over the sorted cookie sizes to find the smallest suitable cookie. We'll mark the cookie as used by logically removing it from the pool (using an increment of the index pointer).
This C solution follows the Binary Search technique. It leverages sorting both arrays, then searching for the smallest valid cookie for each child using binary search. It updates counters and marks cookies to avoid reuse improperly.
Time Complexity: O(n log n + n log m), where n is the number of the greed array, m is the number of cookie size array, originating from sorting and binary searching through the cookies.
Space Complexity: O(1) when not considering input arrays.
| Approach | Complexity |
|---|---|
| Greedy Approach using Sorting | Time Complexity: O(n log n + m log m), where n is the number of children and m is the number of cookies (due to sorting). Space Complexity: O(1), as it uses a constant amount of extra space apart from the input arrays. |
| Greedy Approach with Binary Search | Time Complexity: O(n log n + n log m), where n is the number of the greed array, m is the number of cookie size array, originating from sorting and binary searching through the cookies. Space Complexity: O(1) when not considering input arrays. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy with Sorting + Two Pointers | O(n log n + m log m) | O(1) | Best general solution. Simple, optimal, and commonly expected in interviews. |
| Greedy with Sorting + Binary Search | O(n log n + m log m + n log m) | O(1) | Useful when demonstrating binary search over sorted arrays or when direct pointer scanning is not used. |
L1. Assign Cookies | Greedy Algorithm Playlist • take U forward • 404,726 views views
Watch 9 more video solutions →Practice Assign Cookies with our built-in code editor and test cases.
Practice on FleetCode