Maximum size square sub-matrix with all 1s
Given a binary matrix, find out the maximum size square sub-matrix with all 1s.
For example, consider the below binary matrix.
0 1 1 0 1 1 1 0 1 0 0 1 1 1 0 1-1 1 1 0
1 1 1 1 1 0 0 0 0-0
The maximum square Sub-matrix with the ' Set BITS is '
1 1 1
1 1 1 1 1 1
Algorithm:
Let the given binary matrix is m[r][c]. The idea of the algorithm are to construct a auxiliary size matrix s[][] in which each entry s[i][j] represents size of th E square sub-matrix with all 1s including m[i][j] where m[i][j] is the rightmost and bottommost entry in Sub-matrix.
1) construct a sum matrix S[r][c] for the given m[r][c].
A) Copy-a- and-columns as it is from m[][to s[][]
b for the other entries, use following Expressio NS to construct s[][]
If m[i][j] is 1 then
s[i][j] = min (s[i][j-1], s[i-1][j], s[i-1][j-1]) + 1
Else/*if M[i] [j] is 0*/
s[i][j] = 0
2) Find the maximum entry in S[r][c]
3) Using the value and coordinates of maximum ENT Ry in s[i], print
Sub-matrix of m[][]
For the given m[r][c] in above example, constructed S[r][c] would is:
0 1 1 0 1
1 1 0 1 0 0 1 1 1-0 1 1 2 2 0
1 2 2 3 1 0 0 0 0-0
The value of maximum entry in above matrix is 3 and coordinates of the entry are (4, 3). Using the maximum value and its coordinates, we can find the required Sub-matrix.
http://www.geeksforgeeks.org/maximum-size-sub-matrix-with-all-1s-in-a-binary-matrix/
The main point and Leetcode find the largest area is not the same, here is to find the largest square.
But it's a lot easier than the leetcode on the subject.
Here than the original site province memory, from O (m*n) to O (n).
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/