Taro Logo

Assign Cookies

Easy
7 views
2 months ago

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 * 10^4
  • 0 <= s.length <= 3 * 10^4
  • 1 <= g[i], s[j] <= 2^31 - 1
Sample Answer

The Problem

The problem asks us to maximize the number of content children given their greed factors and the sizes of the cookies available. A child is content if they receive a cookie whose size is greater than or equal to their greed factor. Each child can receive at most one cookie, and each cookie can be given to at most one child.

Naive Solution

A naive solution would be to iterate through all possible assignments of cookies to children and find the assignment that maximizes the number of content children. This would involve generating all permutations of cookie assignments, which is highly inefficient.

def findContentChildren_naive(greed, sizes):
    greed.sort()
    sizes.sort()
    content_children = 0
    size_index = 0
    for g in greed:
        while size_index < len(sizes) and sizes[size_index] < g:
            size_index += 1
        if size_index < len(sizes):
            content_children += 1
            size_index += 1
    return content_children

Optimal Solution

The optimal solution involves sorting both the greed factors and the cookie sizes and then iterating through them using a two-pointer approach. This allows us to efficiently find the maximum number of content children.

def findContentChildren(greed, sizes):
    greed.sort()
    sizes.sort()
    child_index = 0
    cookie_index = 0
    content_children = 0

    while child_index < len(greed) and cookie_index < len(sizes):
        if sizes[cookie_index] >= greed[child_index]:
            content_children += 1
            child_index += 1
        cookie_index += 1

    return content_children

Big(O) Runtime Analysis

The runtime complexity of the optimal solution is dominated by the sorting step, which takes O(n log n) time, where n is the maximum of the number of children and the number of cookies. The two-pointer iteration takes O(n) time. Therefore, the overall runtime complexity is O(n log n).

Big(O) Space Usage Analysis

The space complexity of the optimal solution is O(1) if the sorting is done in place. Otherwise, if the sorting algorithm uses additional space (e.g., merge sort), the space complexity would be O(n).

Edge Cases

  • Empty Input: If either the greed factors or the cookie sizes are empty, the function should return 0.
  • All Children are Greedy: If all children have very high greed factors, the function should return the number of cookies that satisfy at least one child.
  • All Cookies are Small: If all cookies are very small, the function should return the number of children that can be satisfied by the smallest cookie.

Here's how to address edge cases:

def findContentChildren_edge_cases(greed, sizes):
    if not greed or not sizes:
        return 0

    greed.sort()
    sizes.sort()

    child_index = 0
    cookie_index = 0
    content_children = 0

    while child_index < len(greed) and cookie_index < len(sizes):
        if sizes[cookie_index] >= greed[child_index]:
            content_children += 1
            child_index += 1
        cookie_index += 1

    return content_children