KatInterview

455. Assign Cookies

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to find the maximum number of children that can be satisfied with a given set of cookies. Each child has a "greed factor," which is the minimum size of a cookie they will accept. Each cookie has a size. A child is satisfied if they receive a cookie with a size greater than or equal to their greed factor. Each child can get at most one cookie.

Solution

Assign Cookies in Python

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        # Sort both the greed factors and the cookie sizes in ascending order.
        g.sort()
        s.sort()
        
        child_idx = 0  # Pointer for greed factor list g
        cookie_idx = 0 # Pointer for cookie size list s
        
        # Iterate while there are children to satisfy and cookies to give.
        while child_idx < len(g) and cookie_idx < len(s):
            # If the current cookie can satisfy the current child's greed.
            if s[cookie_idx] >= g[child_idx]:
                # The child is content, so we can move to the next child.
                child_idx += 1
            
            # Move to the next cookie in any case.
            # If the cookie was used, we need the next one for the next child.
            # If it was not used, it's too small, so we try the next larger cookie for the current child.
            cookie_idx += 1
            
        # The value of child_idx is the total number of satisfied children.
        return child_idx

Assign Cookies in Java

class Solution {
    public int findContentChildren(int[] g, int[] s) {
        // Sort both the greed factors and the cookie sizes in ascending order.
        Arrays.sort(g);
        Arrays.sort(s);
        
        int childIdx = 0; // Pointer for greed factor array g
        int cookieIdx = 0; // Pointer for cookie size array s
        
        // Iterate while there are children to satisfy and cookies to give.
        while (childIdx < g.length && cookieIdx < s.length) {
            // If the current cookie can satisfy the current child's greed.
            if (s[cookieIdx] >= g[childIdx]) {
                // The child is content, move to the next child to satisfy.
                childIdx++;
            }
            // Move to the next cookie in any case.
            // If the cookie was used, we need the next one for the next child.
            // If it was not used, it's too small, so we try the next larger cookie for the current child.
            cookieIdx++;
        }
        
        // The value of childIdx is the total number of satisfied children.
        return childIdx;
    }
}

Assign Cookies in C++

class Solution {
public:
    int findContentChildren(vector<int>& g, vector<int>& s) {
        // Sort both the greed factors and the cookie sizes in ascending order.
        sort(g.begin(), g.end());
        sort(s.begin(), s.end());

        int child_idx = 0; // Pointer for greed factor array g
        int cookie_idx = 0; // Pointer for cookie size array s
        
        // Iterate while there are children to satisfy and cookies to give.
        while (child_idx < g.size() && cookie_idx < s.size()) {
            // If the current cookie can satisfy the current child's greed.
            if (s[cookie_idx] >= g[child_idx]) {
                // The child is content, move to the next child.
                child_idx++;
            }
            // Move to the next cookie regardless of whether it was used.
            // If used, we need the next cookie for the next child.
            // If not used, we need to try a larger cookie for the current child.
            cookie_idx++;
        }
        
        // The number of satisfied children is the number of children we have successfully assigned cookies to.
        // This is tracked by child_idx.
        return child_idx;
    }
};

Assign Cookies in TypeScript

function findContentChildren(g: number[], s: number[]): number {
    // Sort both the greed factors and the cookie sizes in ascending order.
    g.sort((a, b) => a - b);
    s.sort((a, b) => a - b);
    
    let childIdx = 0; // Pointer for greed factor array g
    let cookieIdx = 0; // Pointer for cookie size array s
    
    // Iterate while there are children to satisfy and cookies to give.
    while (childIdx < g.length && cookieIdx < s.length) {
        // If the current cookie can satisfy the current child's greed.
        if (s[cookieIdx] >= g[childIdx]) {
            // The child is content, so we can move on to the next child.
            childIdx++;
        }
        
        // Move to the next cookie in any case.
        // If the cookie was used, we need the next one for the next child.
        // If it was not used, it's too small, so we try the next larger cookie for the current child.
        cookieIdx++;
    }
    
    // The value of childIdx is the total number of satisfied children.
    return childIdx;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.