FastPrepRemove Equal Components and Apply Gravity

Remove Equal Components and Apply Gravity

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

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 <= 200
  • 0 <= grid[r][c] <= 1000000000

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int[][] removeComponents(int[][] grid) {
    // Write your code here.
}
grid[[1,1,1],[2,0,3],[4,5,6]]
expected[[0,0,0],[2,0,3],[4,5,6]]
Checking account…