Solution: Assign Cookies

Let's solve the Assign Cookies problem using the Greedy Techniques pattern.

Statement

You are given two integer arrays:

  1. greed_factors: This array represents the minimum cookie size required for each child to be content. Specifically, greed_factors[i] denotes the minimum cookie size that child i will accept to be satisfied.

  2. cookie_sizes: This array represents the sizes of the available cookies. For example, cookie_sizes[j] denotes the size of cookie j.

Your objective is to distribute the cookies in a way that maximizes the number of content children. A child i is content if they receive a cookie that is at least as large as their greed factor, i.e., cookie_sizes[j] >= greed_factors[i]. Each child can be assigned at most one cookie, which can only be given to one child.

Write an algorithm to maximize the number of children who can be content by assigning available cookies appropriately.

Constraints:

  • 1≤1 \leq greed_factors.length ≤1000\leq 1000

  • 0≤0 \leq cookie_sizes.length ≤1000\leq 1000

  • 1≤1 \leq greed_factors[i], cookie_sizes[j] ≤105\leq 10^5

Solution

We solve this problem efficiently using a greedy approach. The key is to assign each child the smallest cookie that satisfies their greed factor. First, the greed factors and cookie sizes are sorted in ascending order to ensure we can pair the least greedy child with the smallest possible cookie. The algorithm then uses two pointers: one for the children and one for the cookies. Starting with the least greedy child and the smallest cookie, it checks whether the current cookie can satisfy the child. If it can, both pointers are moved to the next child and cookie; if not, only the cookie pointer advances to find a larger cookie. This process continues until all children have been satisfied or all cookies have been used, maximizing the number of children who can be content.

The steps of the algorithm are as follows:

  1. Sort the greed_factors of the children and the cookie_sizes in ascending order.

  2. Use two pointers, current_child and current_cookie, to track the current child and current cookie, respectively. Additionally, initialize a variable content_children to count how many children are satisfied.

  3. Iterate through both lists using a loop, which will continue as long as there are children and cookies left to consider (current_child < len(greed_factors) and current_cookie < len(cookie_sizes)).

    1. Check if the current cookie can satisfy the current child:

      1. If the current cookie (cookie_sizes[current_cookie]) is large enough to satisfy the current child’s greed factor (greed_factors[current_child]), increment content_children and move both pointers to the next child and the next cookie (current_child++ and current_cookie++).

      2. If the current cookie is not large enough to satisfy the child, move the cookie pointer only to try the next larger cookie (current_cookie++) while considering the same child.

  4. Return the total number of content children once the loop finishes.

Let’s look at the following illustration to get a better understanding of the solution:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.