Assume you are an awesome parent and want to give your children some cookies. 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.
For example:
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. 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.
Another example:
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.
Write a function to determine the maximum number of content children.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to find the maximum number of children we can satisfy with cookies. The brute force way means trying every possible combination of assigning cookies to children to see which gives us the most satisfied kids.
Here's how the algorithm would work step-by-step:
def assign_cookies_brute_force(child_greed_factors, cookie_sizes):
maximum_satisfied_children = 0
def solve(child_index, assigned_cookies, satisfied_children_count):
nonlocal maximum_satisfied_children
# Base case: all children have been considered
if child_index == len(child_greed_factors):
maximum_satisfied_children = max(maximum_satisfied_children, satisfied_children_count)
return
# Explore all possible cookie assignments for the current child
for cookie_index in range(len(cookie_sizes)):
if cookie_index not in assigned_cookies:
# Check if the cookie satisfies the current child.
if cookie_sizes[cookie_index] >= child_greed_factors[child_index]:
# Assign the cookie and recursively explore next child.
assigned_cookies.add(cookie_index)
solve(child_index + 1, assigned_cookies, satisfied_children_count + 1)
assigned_cookies.remove(cookie_index)
# If no cookie satisfies the child, move to the next child.
# The child is not satisfied, check what happens with next child
solve(child_index + 1, assigned_cookies, satisfied_children_count)
solve(0, set(), 0)
return maximum_satisfied_children
The goal is to satisfy as many children as possible with cookies. We want to be efficient and not waste any cookies. The best way to do this is to give the smallest possible cookie to the least greedy child, and move upwards from there.
Here's how the algorithm would work step-by-step:
def assign_cookies(greed_factors, cookie_sizes):
greed_factors.sort()
cookie_sizes.sort()
child_index = 0
cookie_index = 0
number_of_happy_children = 0
while child_index < len(greed_factors) and cookie_index < len(cookie_sizes):
# We want to satisfy the least greedy child with the smallest cookie.
if cookie_sizes[cookie_index] >= greed_factors[child_index]:
number_of_happy_children += 1
child_index += 1
# If the current cookie is too small, move to the next bigger cookie.
cookie_index += 1
return number_of_happy_children
Case | How to Handle |
---|---|
Both greed and sizes arrays are empty | Return 0 since there are no children or cookies. |
Greed array is empty but sizes array is not | Return 0 since there are no children to satisfy. |
Sizes array is empty but greed array is not | Return 0 since there are no cookies to give. |
All children have very large greed factors and all cookies are small | Return 0 since no children can be satisfied. |
All cookies are very large and all children have small greed factors | Return the number of children since all can be satisfied. |
Greed and sizes arrays contain duplicate values | Sorting both arrays ensures the greedy approach can still find the optimal solution by matching smallest greed to smallest cookie. |
Arrays are very large (performance considerations) | The sorting algorithm must be efficient (e.g., O(n log n)) to handle large arrays within time limits. |
Integer overflow if greed factors or sizes are extremely large | The comparison of greed factors and sizes should be done using a data type that prevents overflow (e.g., long in Java or C++ if necessary), but the problem likely constrains the inputs so that normal integers can be used. |