Validate Row and Column Runs of Ones
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[][]) → booleanExamples
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 = trueExact 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 = falseExact 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 = falseThis 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 is0or1. rows.length == Randcolumns.length == C.- For this exercise, assume each clue list has from
1through 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.Kis the total number of supplied clue entries.