Given a binary matrix, find the maximum size rectangle binary-sub-matrix with all 1’s.
Input: 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 Output : 1 1 1 1 1 1 1 1 Explanation : The largest rectangle with only 1's is from (1, 0) to (2, 3) which is 1 1 1 1 1 1 1 1 Input: 0 1 1 1 1 1 0 1 1 Output: 1 1 1 1 1 1 Explanation : The largest rectangle with only 1's is from (0, 1) to (2, 2) which is 1 1 1 1 1 1
There is already an algorithm discussed a dynamic programming based solution for finding largest square with 1s.
Approach: In this post an interesting method is discussed that uses largest rectangle under histogram as a subroutine.
If the height of bars of the histogram is given then the largest area of the histogram can be found. This way in each row, the largest area of bars of the histogram can be found. To get the largest rectangle full of 1’s, update the next row with the previous row and find the largest area under the histogram, i.e. consider each 1’s as filled squares and 0’s with an empty square and consider each row as the base.
Input : 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 Step 1: 0 1 1 0 maximum area = 2 Step 2: row 1 1 2 2 1 area = 4, maximum area becomes 4 row 2 2 3 3 2 area = 8, maximum area becomes 8 row 3 3 4 0 0 area = 6, maximum area remains 8
- Run a loop to traverse through the rows.
- Now If the current row is not the first row then update the row as follows, if matrix[i][j] is not zero then matrix[i][j] = matrix[i-1][j] + matrix[i][j].
- Find the maximum rectangular area under the histogram, consider the ith row as heights of bars of a histogram. This can be calculated as given in this article Largest Rectangular Area in a Histogram
- Do the previous two steps for all rows and print the maximum area of all the rows.
Note: It is strongly recommended to refer this post first as most of the code taken from there.
Area of maximum rectangle is 8
- Time Complexity: O(R x C).
Only one traversal of the matrix is required, so the time complexity is O(R X C)
- Space Complexity: O(C).
Stack is required to store the columns, so so space complexity is O(C)
This article is contributed by Sanjiv Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Submatrix of given size with maximum 1's
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Largest possible square submatrix with maximum AND value
- Maximize the binary matrix by filpping submatrix once
- Maximum value in a matrix which contain intersecting concentric submatrix
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Maximum sum rectangle in a 2D matrix | DP-27
- Maximum size of square such that all submatrices of that size have sum less than K
- Find if there is a rectangle in binary matrix with corners as 1
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Maximum size square sub-matrix with all 1s
- Queries on number of Binary sub-matrices of Given size
- Maximum size square Sub-Matrix with sum less than or equals to K
- Print maximum sum square sub-matrix of given size
- Find size of the largest '+' formed by all ones in a binary matrix
- Maximum product of a triplet (subsequence of size 3) in array
- Find maximum of minimum for every window size in a given array
- Submatrix Sum Queries
- XOR of a submatrix queries