Candy Crush Grid Matching and Gravity
Problem statement
Given a rectangular grid board, a positive integer is a candy type and 0 is empty. A match is a maximal horizontal or vertical run of at least three equal positive values. Detect every match against the original grid, including both runs when horizontal and vertical matches cross.
Implement match3Round(board, part) with two separately observable results:
part = 1: return one row[value, length]for each match. List all horizontal runs first, ordered by row and then starting column. Next list all vertical runs, ordered by column and then starting row. A run of length four produces one row, not two overlapping triples. If there are no matches, return an empty list.part = 2: mark the union of all matched cells before changing the board, clear those cells to zero, then compact each column downward. Keep the relative order of every surviving positive value and fill the remaining top cells with zero. Return the resulting grid after exactly one simultaneous clear-and-gravity round.
The two parts read the same original input independently. A match created by gravity is not cleared again during part 2.
For the source example [[1,2,3,4],[7,7,7,7],[5,2,7,1],[6,2,7,3]], the horizontal four sevens precede the vertical three sevens. After clearing, the grid is [[1,2,3,4],[0,0,0,0],[5,2,0,1],[6,2,0,3]]; applying gravity gives the part-2 example below.
Optional, ungraded practice extension: repeat complete rounds until stable. This extension is not part of either return contract.
Function
match3Round(board: int[][], part: int) → int[][]Examples
Example 1
board = [[1,2,3,4],[7,7,7,7],[5,2,7,1],[6,2,7,3]]part = 1return = [[7,4],[7,3]]Report the horizontal run of four sevens before the vertical run of three. The shared cell belongs to both runs.
Example 2
board = [[1,2,3,4],[7,7,7,7],[5,2,7,1],[6,2,7,3]]part = 2return = [[0,0,0,0],[1,2,0,4],[5,2,0,1],[6,2,3,3]]Remove both runs simultaneously, then drop survivors. The new vertical run of twos is retained because this is one round.
Example 3
board = [[1,2],[3,4]]part = 1return = []Neither direction contains a run of length at least three.
Constraints
1 <= board.length <= 50.1 <= board[i].length <= 50; all rows have the same length.0 <= board[i][j] <= 2147483647.1 <= part <= 2.- Matches use only positive values. Zero interrupts a run and represents an empty cell.