Remove Equal Components and Apply Gravity
Problem statement
Positive cells of equal value form components through shared edges; zero is empty. In one simultaneous round, remove every component containing at least three cells by setting it to zero.
Then compact each column downward toward larger row indices, preserving the top-to-bottom order of surviving values. Return the resulting matrix.
Function
removeComponents(grid: int[][]) → int[][]Examples
Example 1
grid = [[1,1,1],[2,0,3],[4,5,6]]return = [[0,0,0],[2,0,3],[4,5,6]]The three ones disappear and columns compact downward.
Example 2
grid = [[2,1],[2,3],[2,4]]return = [[0,1],[0,3],[0,4]]A vertical three-cell component is removed.
Constraints
1 <= rows, columns <= 2000 <= grid[r][c] <= 1000000000