Maximum Size Rectangle Sub-matrix With All 1's
Posted: 20 Nov, 2020
You are given an 'N' * 'M' sized binary-valued matrix 'MAT, where 'N' is the number of rows and 'M' is the number of columns. You need to return the maximum size (area) of the submatrix which consists of all 1’s i.e. the maximum area of a submatrix in which each cell has only the value ‘1’.
In the above image, areas in green, red, and violet color are all submatrices of the original 4x4 matrix.
1. Binary valued matrix has only two values in each cell : 0 and 1. 2. A submatrix is a matrix formed by selecting certain rows and columns from a larger matrix. 3. The area of a matrix with 'h' rows and 'w' columns is equal to 'h' * 'w'.
The first line of the input contains an integer 'T' denoting the number of test cases. The first line of each test case contains two space-separated integers 'N' and 'M', where 'N' = the number of rows in the given matrix and 'M' = the number of columns in the given matrix. Then 'N' lines follow for each test case. Each line contains 'M' space-separated integers (either 1 or 0) denoting matrix elements.
For each test case print in a single line the area of maximum size submatrix of all 1’s in the given matrix on a new line. The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= 'T' <= 50 1 <= 'N', 'M' <= 100 Time Limit: 1 sec
- We start from the first row and move downwards.
- We create three 1-dimensional arrays HEIGHT, LEFT, RIGHT.
- ‘HEIGHT’[i]: stores the number of current continuous 1’s in column i.
- LEFT[i] : stores the leftmost index ‘j’ such that all indices say ‘K’, ‘K’ belongs to [j, i], ‘HEIGHT’[k] >= ‘HEIGHT’[i].
- RIGHT [i]: stores the rightmost index ‘j’ such that all indices say ‘K’, ‘K’ belongs to [i, j], ‘HEIGHT’[k] >= ‘HEIGHT’[i].
- By the above definitions, it’s easier to figure out that we can update our maximum area with the value (‘HEIGHT’[i] * (RIGHT [i] - LEFT [i])).
- Now let’s think how will we update the above three 1-dimensional arrays itself.
- Initialize the ‘HEIGHT’ array with 0.
- Iterate through the rows from 0 to N-1.
- For each column:
- If the current value (MAT [i][j]) is 1, then update ‘HEIGHT’[j]++.
- Else reset the value of ‘HEIGHT’[j] to 0.
- Note that:
- Initially, ‘HEIGHT’ was initialized to 0. So LEFT array should be initialised to 0 as per definition.
- And similarly, RIGHT array should be initialized to M.
- Updating the LEFT array in each iteration among rows 0 to N-1:
- We scan from left to right.
- Initialize LEFTBOUNDARY = 0, which means left boundary for all 1’s in the current row.
- Iterate in the current row:
- If the current value (MAT [i][j]) is 1, then you can reset the LEFT[j] from 0 to LEFTBOUNDARY.
- Else left[j] = 0 and update LEFTBOUNDARY to j+1. As the current value is 0, so next LEFTBOUNDARY for all the cells in the Matrix ahead of column j is at least j+1.
- Similarly, for the RIGHT array in each iteration among rows 0 to N-1:
- We scan from right to left.
- Initialize RIGHTBOUNDARY = 0, which means right boundary for all 1’s in the current row.
- Iterate in the current row:
- If the current value (MAT[i][j]) is 1, then you can reset the RIGHT[j] from M to RIGHTBOUNDARY.
- Else RIGHT[j] = 0 and update RIGHTBOUNDARY to j-1. As current value is 0, so next RIGHTBOUNDARY for all the cells in the Matrix before column j is atmost j-1.
Special Cells in Binary Matrix
Posted: 8 Jan, 2022
Posted: 9 Jan, 2022
Ninja And The Rows
Posted: 14 Jan, 2022
Set Matrix Zeros
Posted: 25 Jan, 2022
Posted: 19 Apr, 2022