Maximum size square sub-matrix with all 1s Geeks face test

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.