Greedy Algorithms8 sections · 316 units
Open in Course

Assign Cookies - Walkthrough

Tracing the algorithm

Trace with greed = [1, 2, 3] and cookies = [1, 2, 3]: Sorted: greed = [1, 2, 3], cookies = [1, 2, 3] 1.1. Child 00 (greed 11), cookie 00 (size 11): 11 >= 11, satisfied! Move both.

2.2. Child 11 (greed 22), cookie 11 (size 22): 22 >= 22, satisfied! Move both.

3.3. Child 22 (greed 33), cookie 22 (size 33): 33 >= 33, satisfied! Move both. Answer: 33 children satisfied. Notice each cookie satisfied exactly one child. Had we used larger cookies on smaller greeds, we would have wasted capacity.