Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Cookie Pact

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Give each child the smallest sufficient gift: The Cookie Pact.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Sorted greedy matching matches the clues
  2. 2Keep this true after every move: matched children use the smallest cookies capable of satisfying them
  3. 3Reach the result within O(n log n + m log m)

Rules and pressure

  • Target cost: O(n log n + m log m)
  • State rule: matched children use the smallest cookies capable of satisfying them
Lesson 1 of 3

Live algorithm trace

Sorted greedy matching

Complete execution
1 of 4

Sort both sides and compare their smallest remaining values.

1sort greed and cookie sizes
2child = cookie = 0
3if cookie is large enough: match child
4always advance cookie
5return matched children
greed = [1,2,3]cookies = [1,1]matched = 0
Truth to preserve / Cost target
Truth to preserve

matched children use the smallest cookies capable of satisfying them

Cost target

O(n log n + m log m)

Process children by increasing greed and cookies by increasing size. A too-small cookie can satisfy nobody later; the first sufficient cookie should serve the least-demanding remaining child.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead