Skip to content
StackDecode
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

  1. A child is content only with a cookie at least as big as their greed.
  2. Sort both lists.
  3. 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.
  4. Never waste a big cookie on a small appetite. O(n log n).

Watch it run

Loading animation…