Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources
Assign Cookies

Assign Cookies

Problem Statement

You’re a cookie distributor with a mission! Given an array of children’s greed factors (minimum cookie size they’ll accept) and an array of cookie sizes, assign cookies to maximize the number of happy children. Each child gets at most one cookie, and the cookie’s size must meet or exceed their greed factor. This easy-level greedy problem is like playing matchmaker between kids and treats—find the best pairs to spread the most joy!

Example

Input: g = [1, 2, 3] (greed factors), s = [1, 1] (cookie sizes)

Output: 1 (only one child can be satisfied, e.g., greed 1 gets cookie 1)

Input: g = [1, 2], s = [1, 2, 3]

Output: 2 (both children satisfied: greed 1 gets cookie 1, greed 2 gets cookie 2)

Input: g = [10, 9, 8, 7], s = [5, 6, 7, 8]

Output: 2 (greed 7 gets cookie 7, greed 8 gets cookie 8)

Code

Java
Python
JavaScript
public class Solution {
    public int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g); // Sort greed factors
        Arrays.sort(s); // Sort cookie sizes
        
        int childIndex = 0;
        int cookieIndex = 0;
        int contentChildren = 0;
        
        while (childIndex < g.length && cookieIndex < s.length) {
            if (s[cookieIndex] >= g[childIndex]) {
                contentChildren++;
                childIndex++;
                cookieIndex++;
            } else {
                cookieIndex++;
            }
        }
        return contentChildren;
    }
}
            
def find_content_children(g, s):
    g.sort()  # Sort greed factors
    s.sort()  # Sort cookie sizes
    
    child_idx = 0
    cookie_idx = 0
    content = 0
    
    while child_idx < len(g) and cookie_idx < len(s):
        if s[cookie_idx] >= g[child_idx]:
            content += 1
            child_idx += 1
            cookie_idx += 1
        else:
            cookie_idx += 1
    return content

# Example usage
print(find_content_children([1, 2, 3], [1, 1]))      # 1
print(find_content_children([1, 2], [1, 2, 3]))      # 2
print(find_content_children([10, 9, 8, 7], [5, 6, 7, 8]))  # 2
            
function findContentChildren(g, s) {
    g.sort((a, b) => a - b); // Sort greed factors
    s.sort((a, b) => a - b); // Sort cookie sizes
    
    let childIdx = 0;
    let cookieIdx = 0;
    let content = 0;
    
    while (childIdx < g.length && cookieIdx < s.length) {
        if (s[cookieIdx] >= g[childIdx]) {
            content++;
            childIdx++;
            cookieIdx++;
        } else {
            cookieIdx++;
        }
    }
    return content;
}

// Example usage
console.log(findContentChildren([1, 2, 3], [1, 1]));        // 1
console.log(findContentChildren([1, 2], [1, 2, 3]));        // 2
console.log(findContentChildren([10, 9, 8, 7], [5, 6, 7, 8])); // 2
            

Explanation

  • Greedy Strategy: Sort both arrays to pair the smallest cookies with the least greedy children first.
  • Two Pointers: Use indices to track the current child and cookie, moving them based on matches.
  • Match Process: If a cookie satisfies a child (size ≥ greed), count it and advance both pointers; otherwise, skip the cookie.
  • Flow Example (g=[1,2], s=[1,2,3]): Start with child 1, cookie 1 → match (count 1); child 2, cookie 2 → match (count 2); cookie 3 unused.
  • Termination: Stop when either all children are satisfied or cookies run out.

Note

Time complexity is O(n log n) due to sorting, where n is the maximum length of g or s. Space complexity is O(1) as it uses only a few variables. A sweet and simple greedy win!