This question is about implementing a basic elimination algorithm for Candy Crush.
Given an m x n integer array board representing the grid of candy where board[i][j] represents the type of candy. A value of board[i][j] == 0 represents that the cell is empty.
The given board represents the state of the game following the player's move. Now, you need to restore the board to a stable state by crushing candies according to the following rules:
You need to perform the above rules until the board becomes stable, then return the stable board.
Example 1:
Input: board = [[110,5,112,113,114],[210,211,5,213,214],[310,311,3,313,314],[410,411,412,5,414],[5,1,512,3,3],[610,4,1,613,614],[710,1,2,713,714],[810,1,2,1,1],[1,1,2,2,2],[4,1,4,4,1014]] Output: [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[110,0,0,0,114],[210,0,0,0,214],[310,0,0,113,314],[410,0,0,213,414],[610,211,112,313,614],[710,311,412,613,714],[810,411,512,713,1014]]
Example 2:
Input: board = [[1,3,5,5,2],[3,4,3,3,1],[3,2,4,5,2],[2,4,4,5,5],[1,4,4,1,1]] Output: [[1,3,0,0,0],[3,4,0,5,2],[3,2,0,3,1],[2,4,0,5,2],[1,4,3,1,1]]
Constraints:
m == board.lengthn == board[i].length3 <= m, n <= 501 <= board[i][j] <= 2000Problem Overview: You’re given a 2D board representing candies. Whenever three or more identical candies appear consecutively in a row or column, they get crushed (set to zero), and the candies above fall down due to gravity. This process repeats until no more matches exist. The goal is to return the final stable board.
Approach 1: Naive Re-scan Simulation (O((m*n)^2) time, O(1) space)
The most direct strategy is to repeatedly scan the grid and remove matches as soon as you find them. Iterate through the matrix, check every cell for horizontal or vertical runs of length ≥ 3, mark them as crushed, then shift candies down column by column. After each crush, run another full scan because new matches may form after gravity applies.
This works but does extra work because the board may change multiple times per iteration. In the worst case, each round only removes a few candies, causing many full rescans. The approach still uses constant extra space because updates happen directly on the board.
Approach 2: Mark-and-Drop Simulation (O(m*n*max(m,n)) time, O(1) space)
A cleaner simulation separates detection and removal. First pass: scan the grid and mark candies that should be crushed instead of removing them immediately. When checking horizontally or vertically, compare absolute values so previously marked cells are still counted. Mark cells by negating their value or using a sentinel. This ensures overlapping groups are handled in the same pass.
Second pass: apply gravity column by column. Use a pointer from the bottom of each column and move surviving candies downward. Fill remaining cells at the top with zeros. This step resembles a compaction pass commonly used in array problems.
Repeat the two phases until no new candies are marked. The key insight is that marking first prevents interference between overlapping matches in the same iteration. Each round processes the entire matrix once for detection and once for gravity.
Recommended for interviews: The mark-and-drop simulation is the expected solution. It shows you can model state changes cleanly while avoiding incorrect partial updates. A brute re-scan demonstrates understanding of the mechanics, but the marking technique proves you can design robust simulations on grid problems.
We can traverse the matrix row by row and column by column to find three consecutive identical elements and mark them as negative numbers. If marking is successful, we need to move the elements in the matrix down until no elements can move down.
The time complexity is O(m^2 times n^2), where m and n are the number of rows and columns of the matrix, respectively. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Naive Re-scan Simulation | O((m*n)^2) | O(1) | Simple implementation when correctness matters more than efficiency |
| Mark-and-Drop Simulation | O(m*n*max(m,n)) | O(1) | Optimal simulation approach for grid problems with repeated state updates |
CANDY CRUSH (Leetcode) - Code & Whiteboard • babybear4812 • 18,581 views views
Watch 7 more video solutions →Practice Candy Crush with our built-in code editor and test cases.
Practice on FleetCode