Ctrl K

Match-Three Cascade

Medium
You are given a match-three game board, a matrix board of size m × n, where board[i][j] is a tile color and 0 means an empty cell. Row 0 is the top of the board. Bring the board to a stable state by repeating rounds: 1. Find every run of three or more equal tiles in a row, horizontally or vertically. All those tiles disappear at the same time (a tile may belong to a horizontal and a vertical run at once). 2. In every column, the remaining tiles fall down until they land on the bottom or on another tile. No new tiles come in from the top; freed cells stay empty. Once a round has nothing to remove, the board is stable. Return it.
Example 1
board=[[1, 2, 3], [4, 2, 5], [7, 2, 7], [6, 6, 6]] → [[0, 0, 0], [1, 0, 3], [4, 0, 5], [7, 0, 7]]

The column of three 2s and the row of three 6s vanish at the same time. The remaining tiles fall down and no new matches appear.

Example 2
board=[[6, 2, 3], [6, 4, 4], [1, 1, 1], [6, 5, 7]] → [[0, 0, 0], [0, 2, 3], [0, 4, 4], [0, 5, 7]]

In the first round the three 1s disappear. After the fall, the three 6s in the left column stack up, so they vanish in the second round. Nothing else matches after that.

Example 3
board=[[1, 2, 1], [2, 1, 2], [1, 2, 1]] → [[1, 2, 1], [2, 1, 2], [1, 2, 1]]

There are no three equal tiles in a row anywhere, so the board is already stable.

Constraints
  • 3 ≤ m, n ≤ 50
  • 1 ≤ board[i][j] ≤ 2000 (the initial board has no empty cells)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMirror and Negate a Bitmap EasySpiral Readout MediumSpiral-Numbered Square MediumNext Step of a Cell Colony MediumZigzag Diagonal Sweep Medium
loading Python…
Input
board=[[1, 2, 3], [4, 2, 5], [7, 2, 7], [6, 6, 6]]
Expected
[[0, 0, 0], [1, 0, 3], [4, 0, 5], [7, 0, 7]]

Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.