Greedy Algorithms8 sections · 316 units
Open in Course

Bag of Tokens - Two Pointer Greedy

Small face-up, large face-down

Sort tokens. Use two pointers: left for face-up, right for face-down. Greedily play the smallest token face-up (costs least power).

If you cannot afford any face-up and have score to spare, play the largest token face-down (gains most power).

This maximizes power gained per score spent, letting you play more tokens face-up overall. Why this greedy choice is optimal: maximizing the number of face-up plays maximizes score. Each face-down play is a sacrifice to enable more face-up plays. By trading the largest token for power, you gain the most power per score spent, enabling the most future face-up plays.