Skip to main content
PreparationZone Logo
PreparationZone
Back to Coding

Coding · Easy · Free

Assign cookies to children — maximize satisfied children

CodingGreedyFresher
Model answer

Approach

  • Sort children greed and cookies size.
  • Try smallest cookie that satisfies current child; move pointers.

Java Solution

Java
public int findContentChildren(int[] g, int[] s) {
    Arrays.sort(g);
    Arrays.sort(s);
    int i = 0, j = 0, happy = 0;
    while (i < g.length && j < s.length) {
        if (s[j] >= g[i]) { happy++; i++; }
        j++;
    }
    return happy;
}
Complexity
TimeO(n log n)
SpaceO(1)
Interview tip: Classic two-pointer greedy after sorting.