Minimum Boards to Cover Roof Holes
Problem statement
An m x n binary matrix roof describes a roof. A cell containing 0 is a hole, while a cell containing 1 is intact.
You may place either kind of board:
- A horizontal
1 x nboard covers every cell in one chosen row. - A vertical
m x 1board covers every cell in one chosen column.
Boards may overlap. Return the minimum number of boards needed so every hole is covered.
Function
minimumRoofBoards(roof: int[][]) → intExamples
Example 1
roof = [[1,0,1],[0,1,0]]return = 2Cover row 1 and column 1. Together they cover all three holes.
Example 2
roof = [[1,1],[1,1]]return = 0An intact roof needs no board.
Example 3
roof = [[0,0,0],[0,0,0]]return = 2Two horizontal boards cover the complete roof, which is fewer than the three vertical boards.
Constraints
1 <= m, n <= 300.- Every row has exactly
ncells. - Every cell is
0or1.