221. Maximal Square
Reference: https://discuss.leetcode.com/topic/20801/extremely-simple-java-solution
Thanks jaqenhgar ! Really F**king Genius.
1. Description:
Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area.
For example, given the following matrix:
1 | 1 0 1 0 0 |
Return 4.
2. Difficulty:
Medium
3. Relative Problems:
1 | 84. Largest Rectangle in Histogram |
4. Solution:
Input 2D matrix. Return int maxArea.
Main Idea: Dynamic Programming.
Cause it is a Dynamic Programming question, we should find the state equation.
So I create a 2D matrix edge to store the length of edge of square.(As the point is in the bottom right)
For an element which = 1, I consider it as a square which edge = 1.
To find a square, we need to check ( i, j - 1 ) , (i - 1, j) , (i - 1, j - 1).
If it is all 1, ? = 2.
1 | 1 1 |
If it is one element = 0 , ? = 1, we can not get a bigger square!
1 | 1 0 |
If it is all 2, ? = 3.
1 | 1 1 1 |
You could try this matrix to see whether you can get correct edge, and it would be much more clear for us to understand !
1 | 1 0 1 0 0 |
For such matrix, we could get edge
1 | 0 0 0 0 0 0 |
6. Code
1 | if(matrix.length == 0) |