GreedyAnimated
Assign Cookies
Give the smallest sufficient cookie to the least greedy child.
Time
O(n log n)
Extra space
O(1)
Use it for
Matching with thresholds.
How it works
- A child is content only with a cookie at least as big as their greed.
- Sort both lists.
- Offer the smallest remaining cookie to the least greedy remaining child. If it is big enough, they are content and both move on; otherwise try the next, bigger cookie.
- Never waste a big cookie on a small appetite. O(n log n).
Watch it run
Loading animation…