Assign Cookies
Child i is content if they get a cookie of size at least their greed g[i]. Each child gets at most one cookie. Maximize the number of content children.
Intuition
Sort both lists and hand the smallest useful cookie to the least greedy child. A big cookie spent on an easily-pleased kid is a waste — matching small with small keeps the large cookies for the kids who actually need them.
Approach
Why greedy is safe here
Suppose an optimal solution gives the least greedy unfed child a bigger cookie than necessary. Swap it with the smallest cookie that satisfies them — nothing breaks, and the bigger cookie is freed for someone greedier. Repeating this argument turns any optimal solution into the greedy one, so greedy is optimal.
Two pointers over two sorted lists
Sort g (greed) and s (cookies). Walk the cookies smallest-first with a pointer child into the greed list: if the current cookie satisfies g[child], feed them and advance child; otherwise the cookie is too small for everyone remaining (they're sorted!) — discard it and try the next.
The child pointer is the answer
Every advance of child is one content kid, and no cookie is ever reconsidered. One pass after sorting: O(n log n + m log m) time, O(1) extra space. child ends as the count.
Solution & live demo
Edge cases
The loop body never feeds anyone — return 0.
Every comparison fails; child never advances; answer 0.
Once child == len(g) everyone is fed — break early; extra cookies are irrelevant.