FastPrepCoordinate Removals with Column Gravity

Coordinate Removals with Column Gravity

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

Problem statement

matrix contains distinct positive values; zero represents empty. Process zero-based [row,column] coordinates in order against the evolving matrix.

If the addressed cell is nonzero, remove it, shift every value above it down by one in that column, and set the top cell to zero. If it is already zero, skip it. Return the final matrix.

Function

removeCellsWithGravity(matrix: int[][], coordinates: int[][]) → int[][]

Examples

Example 1

matrix = [[1,2],[3,4]]coordinates = [[1,0]]return = [[0,2],[1,4]]

The value above falls into the removed bottom cell.

Example 2

matrix = [[1],[2],[3]]coordinates = [[0,0]]return = [[0],[2],[3]]

Removing the top simply replaces it with zero.

Constraints

  • 1 <= rows, columns <= 500
  • 0 <= coordinates.length <= 100000
  • Every coordinate is in bounds.

More ZipRecruiter problems

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