Problem · Array

Candy Crush Grid Matching and Gravity

MediumRobloxPHONE SCREEN
See Roblox hiring insights

You are given an m x n grid board of non-negative integers. A positive value represents a candy type, and 0 represents an empty cell. A match is any horizontal or vertical run of at least three adjacent cells with the same positive value.

Part 1 — find matches. Identify every horizontal and vertical run of length at least 3. Scan start cells in row-major order (top-left to bottom-right). A vertical run is only started from a cell when the cell above it differs or is out of bounds; a horizontal run is only started when the cell to the left differs or is out of bounds. Several reports note a direction-priority requirement: report all vertical matches before horizontal ones.

Part 2 (the main ask) — crush and apply gravity. Return the board after one crush pass: remove every cell that belongs to at least one match (set it to 0), then apply gravity independently in each column so that non-zero values fall downward while preserving their relative order within the column, and empty cells at the top become 0. Important: mark all matched cells first (using a separate marker grid) before zeroing anything, so overlapping and adjacent matches are all detected against the original board.

Follow-up: repeat the crush-and-gravity step until the board is stable (no run of length >= 3 remains), which is the full LeetCode 723 "Candy Crush" variant.

Examples
01 · Example 1
board = [[1, 2, 2, 2], [1, 3, 4, 5], [1, 3, 3, 3], [6, 7, 8, 9]]
return = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 3, 4, 5], [6, 7, 8, 9]]
Matches: the first column has three 1s vertically, row 0 has three 2s horizontally, and row 2 has three 3s horizontally. Those matched cells are removed, then each column compacts downward: column 0 keeps only 6, column 1 keeps 3 then 7, column 2 keeps 4 then 8, column 3 keeps 5 then 9, with zeros filling the top.
02 · Example 2
board = [[1, 1, 1], [2, 4, 5], [2, 6, 7]]
return = [[0, 0, 0], [2, 4, 5], [2, 6, 7]]
Row 0 is a horizontal run of three 1s and is crushed to zeros. The two 2s in column 0 form a run of length 2 only, so they are not a match and survive. After crushing the top row, gravity pulls each column's surviving values down: column 0 already has its 2s at the bottom, and columns 1 and 2 are unaffected.
03 · Example 3
board = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
return = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
There is no run of three equal positive values in any row or column, so nothing is crushed and the board is returned unchanged.
Constraints
  • 1 <= m, n <= 50
  • 0 <= board[i][j] (a positive value is a candy type; 0 is empty).
  • A match is a horizontal or vertical run of at least 3 equal positive values.
  • Gravity is applied per column, preserving the relative order of surviving values.
More Roblox problems
drafts saved locally
public int[][] crushOnce(int[][] board) {
  // write your code here
}
board[[1, 2, 2, 2], [1, 3, 4, 5], [1, 3, 3, 3], [6, 7, 8, 9]]
expected[[0, 0, 0, 0], [0, 0, 0, 0], [0, 3, 4, 5], [6, 7, 8, 9]]
sign in to submit