FastPrepValidate Row and Column Runs of Ones

Validate Row and Column Runs of Ones

Indeed logoIndeed● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a rectangular binary grid, return whether the supplied rows and columns describe every line exactly.

Each clue list gives, in order, the lengths of maximal consecutive runs of 1. Read rows left to right and columns top to bottom. Separate runs require at least one 0; leading and trailing zeros do not create groups. A line with no ones is represented by exactly [0].

Both the row and column clues must match. Validate the clues as supplied; do not change them to fit the grid.

For this exercise, assume nonempty rectangular binary input, one clue list per row/column, and a boolean return. Do not modify any input. Each clue list is nonempty and contains positive lengths, or is exactly [0]; empty lists and mixed zero/positive lists are outside the input contract.

Function

validateGroups(grid: int[][], rows: int[][], columns: int[][]) → boolean

Examples

Example 1

grid = [[0,0,1,1,0,1],[0,1,1,0,0,1],[0,0,0,0,0,0],[0,1,0,1,0,1]]rows = [[2,1],[2,1],[0],[1,1,1]]columns = [[0],[1,1],[2],[1,1],[0],[2,1]]return = true

Exact source example: every row and column sequence matches. The all-zero third row and first/fifth columns use [0].

Example 2

grid = [[0,0],[1,0]]rows = [[1],[0]]columns = [[1],[0]]return = false

Exact source example: columns match, but actual row sequences are [0] and [1], reversing the supplied row clues. The result is false.

Example 3

grid = [[1,0],[0,1]]rows = [[1],[1]]columns = [[2],[0]]return = false

This authored example has matching rows and incorrect columns. Both directions must match, so the result is false.

Constraints

  • For this exercise, assume 1 <= R,C <= 30, all grid rows have equal length, and every cell is 0 or 1.
  • rows.length == R and columns.length == C.
  • For this exercise, assume each clue list has from 1 through its line length entries, all positive and no greater than that line length, or is the sole list [0].
  • Positive clue lists may be infeasible; any mismatch returns false. K is the total number of supplied clue entries.

More Indeed problems

See Indeed hiring insights
public boolean validateGroups(int[][] grid, int[][] rows, int[][] columns) {
    // Write your code here.
    return false;
}
grid[[0,0,1,1,0,1],[0,1,1,0,0,1],[0,0,0,0,0,0],[0,1,0,1,0,1]]
rows[[2,1],[2,1],[0],[1,1,1]]
columns[[0],[1,1],[2],[1,1],[0],[2,1]]
expectedtrue
Checking account…