Coding · Easy · Free
Assign cookies to children — maximize satisfied children
CodingGreedyFresher
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 | |
|---|---|
| Time | O(n log n) |
| Space | O(1) |
Interview tip: Classic two-pointer greedy after sorting.