Assign Cookies

Pattern: Candy and assign cookies

easy

Sort the children by greed and the cookies by size. Walk the cookies from smallest up; give each one to the least greedy child still waiting if it is big enough, otherwise it is too small for everyone and is skipped.

The problem

Child i is happy with a cookie of size at least g[i]; each child gets at most one cookie, and cookie j has size s[j]. Return the most happy children. g = [1, 2, 3], s = [1, 1] gives 1.

Think first

  • Which child is easiest to please?
  • Which cookie should that child get?
  • What do you do with a cookie that is too small for the least greedy child?

Hints

    Brute force, and why it fails

    Try every assignment: factorial.

    The key insight

    Matching sorted lists in order never wastes a big cookie on a small appetite, so it gives the most matches.

    Dry run

    child0

    g = [1, 2, 3], s = [1, 1]. Sorted already.

    Step 1 of 3

    The plan

    sort g; sort s; child = 0
    for cookie in s: if child < len(g) and cookie >= g[child]: child += 1
    return child
    

    Complexity

    Time
    O(n log n + m log m)
    Space
    O(n + m)

    Two sorts and a linear scan.

    Edge cases

    • No cookies: 0.
    • Cookies bigger than every greed: min(n, m).
    • Equal values.
    I've built my logic, show the code

    Java

    import java.util.*;
    
    class Solution {
        public int findContentChildren(int[] g, int[] s) {
            int[] greed = g.clone(), size = s.clone();
            Arrays.sort(greed);
            Arrays.sort(size);
            int child = 0;
            for (int cookie = 0; cookie < size.length && child < greed.length; cookie++) {
                if (size[cookie] >= greed[child]) child++;   // the smallest cookie that satisfies the least greedy child
            }
            return child;
        }
    }

    C++

    class Solution {
    public:
        int findContentChildren(vector<int>& g, vector<int>& s) {
            vector<int> greed(g), size(s);
            sort(greed.begin(), greed.end());
            sort(size.begin(), size.end());
            size_t child = 0;
            for (size_t cookie = 0; cookie < size.size() && child < greed.size(); cookie++) {
                if (size[cookie] >= greed[child]) child++;   // the smallest cookie that satisfies the least greedy child
            }
            return static_cast<int>(child);
        }
    };

    Python

    class Solution:
        def findContentChildren(self, g: list[int], s: list[int]) -> int:
            greed, size = sorted(g), sorted(s)
            child = 0
            for cookie in size:
                if child < len(greed) and cookie >= greed[child]:
                    child += 1                      # the smallest cookie that satisfies the least greedy child
            return child

    Common mistakes

    • Giving the biggest cookie to the first child.
    • Moving to the next child when a cookie fails, instead of the next cookie.

    Similar problems