"Leetcode" maximal Square

Source: Internet
Author: User

Title Link: https://leetcode.com/problems/maximal-square/

Topic:

Given a 2D binary matrix filled with 0 's and 1 ' s, find the largest square containing all 1 's and return its area.

For example, given the following matrix:

1 1 1 1 11 0 0 1 0
Return 4.

Ideas:

With C[i][j] is expressed in a matrix of length I, Width j, containing matrix[i][j] this point of the largest square of the length.

Obviously C[i][j] is about the size of c[i-1][j], c[i][j-1], c[i-1][j-1]. The area surrounded by c[i-1][j], c[i][j-1], c[i-1][j-1].

C[i][j] = min (C[i-1][j], c[i][j-1], c[i-1][j-1]) +1

Algorithm:

public int maximalsquare (char[][] matrix) {          if (matrix.length = = 0)              return 0;          int max = 0;          int c[][] = new Int[matrix.length][matrix[0].length];          for (int i = 0, i < matrix.length; i++) {for              (int j = 0; J < Matrix[0].length; J + +) {                  if (matrix[i][j] = =) 0 ') {                      continue;                  }                  if (i = = 0 | | j = 0) {//at Edge                      c[i][j] = 1;                  } else {                      c[i][j] = Math.min (C[i-1][j-1], math.min (c[i-1][ J], C[i][j-1])) + 1;                  }                  max = Math.max (max, c[i][j]);              }          }          return Max * MAX;      }  


"Leetcode" maximal Square

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.