SDE & MLE Prep

455. Assign Cookies

Problem

Child i is content with a cookie of size at least g[i]; cookie j has size s[j]. Each child gets at most one cookie. Return the maximum number of content children.

Example: g = [1,2,3], s = [1,1] → 1; g = [1,2], s = [1,2,3] → 2

Sort both. Give the smallest cookie that satisfies the least greedy child; a cookie too small for this child is too small for everyone after.

Complexity: O(n log n).

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort(); s.sort()
        child = 0
        for size in s:
            if child < len(g) and size >= g[child]:
                child += 1
        return child