Greedy Algorithms8 sections · 316 units
Open in Course

Candy Crush - Algorithm

Mark, crush, drop

The algorithm has three phases per round:

1.1. Mark:: Find all horizontal and vertical runs of 33+ same candies. Mark them for removal.

2.2. Crush:: Remove all marked candies (set to 00 or empty).

3.3. Drop:: Apply gravity. Each column's candies fall to fill empty spaces. Repeat until no candies are marked. The greedy aspect: crush everything possible before dropping. Common bug: crushing one match at a time instead of all at once can miss cascading matches. Time: O(n)O(n). Space: O(n)O(n).