-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
226 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
20241003: 524 | ||
20241003: 524,221 | ||
20241002: 743,889 | ||
20241001: 837 | ||
20240929: 981 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MaximalSquare.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package LeetCodeJava.DynamicProgramming; | ||
|
||
// https://leetcode.com/problems/maximal-square/description/ | ||
/** | ||
* 221. Maximal Square | ||
* | ||
* Medium | ||
* Topics | ||
* Companies | ||
* Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. | ||
* | ||
* | ||
* | ||
* Example 1: | ||
* | ||
* | ||
* Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] | ||
* Output: 4 | ||
* Example 2: | ||
* | ||
* | ||
* Input: matrix = [["0","1"],["1","0"]] | ||
* Output: 1 | ||
* Example 3: | ||
* | ||
* Input: matrix = [["0"]] | ||
* Output: 0 | ||
* | ||
* | ||
* Constraints: | ||
* | ||
* m == matrix.length | ||
* n == matrix[i].length | ||
* 1 <= m, n <= 300 | ||
* matrix[i][j] is '0' or '1'. | ||
* | ||
*/ | ||
public class MaximalSquare { | ||
// V0 | ||
// TODO : implement | ||
// public int maximalSquare(char[][] matrix) { | ||
// | ||
// } | ||
|
||
|
||
// V1 | ||
// IDEA : DP | ||
// https://leetcode.com/problems/maximal-square/solutions/61876/accepted-clean-java-dp-solution/ | ||
public int maximalSquare_1(char[][] a) { | ||
if (a == null || a.length == 0 || a[0].length == 0) | ||
return 0; | ||
|
||
int max = 0, n = a.length, m = a[0].length; | ||
|
||
// dp(i, j) represents the length of the square | ||
// whose lower-right corner is located at (i, j) | ||
// dp(i, j) = min{ dp(i-1, j-1), dp(i-1, j), dp(i, j-1) } | ||
int[][] dp = new int[n + 1][m + 1]; | ||
|
||
for (int i = 1; i <= n; i++) { | ||
for (int j = 1; j <= m; j++) { | ||
if (a[i - 1][j - 1] == '1') { | ||
dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i][j - 1])) + 1; | ||
max = Math.max(max, dp[i][j]); | ||
} | ||
} | ||
} | ||
|
||
// return the area | ||
return max * max; | ||
} | ||
|
||
// V2 | ||
// IDEA : DP | ||
// https://leetcode.com/problems/maximal-square/solutions/61805/evolve-from-brute-force-to-dp/ | ||
public int maximalSquare_2(char[][] matrix) { | ||
int r=matrix.length; | ||
if(r==0) return 0; | ||
int c=matrix[0].length,edge=0; | ||
int[][] dp=new int[r+1][c+1]; | ||
for(int i=1;i<=r;i++) | ||
for(int j=1;j<=c;j++) { | ||
if(matrix[i-1][j-1]=='0') continue; | ||
dp[i][j]=1+Math.min(dp[i-1][j],Math.min(dp[i-1][j-1],dp[i][j-1])); | ||
edge=Math.max(edge,dp[i][j]); | ||
} | ||
return edge*edge; | ||
} | ||
|
||
// V3_1 | ||
// IDEA : DP | ||
// https://leetcode.com/problems/maximal-square/solutions/61828/my-java-dp-ac-solution-simple-and-easy-to-understand-with-explanation/ | ||
public int maximalSquare_3_1(char[][] matrix) { | ||
|
||
//illegal check - no square can be formed | ||
if(matrix == null || matrix.length == 0) return 0; | ||
|
||
int result = 0; | ||
int[][] count = new int[matrix.length][matrix[0].length]; | ||
|
||
//initialize first row and first column | ||
for(int i = 0; i < matrix.length; i ++) { | ||
count[i][0] = matrix[i][0] == '0' ? 0 : 1; | ||
result = Math.max(result, count[i][0]); | ||
} | ||
|
||
for(int i = 0; i < matrix[0].length; i ++) { | ||
count[0][i] = matrix[0][i] == '0' ? 0 : 1; | ||
result = Math.max(result, count[0][i]); | ||
} | ||
|
||
//start to transfer status to iterate each cell from (1, 1) to (m, n) | ||
//if i am a 0, the square stops, reset | ||
for(int i = 1; i < matrix.length; i++) { | ||
for(int j = 1; j < matrix[0].length; j++) { | ||
|
||
//I break the square reset myself to zero | ||
if(matrix[i][j] == '0') { | ||
count[i][j] = 0; | ||
continue; | ||
} | ||
|
||
//if I am 1, it depends if I can grow the size of the square, if I have a 0 guy around me, | ||
//I can only be a top left guy | ||
if(count[i - 1][j - 1] == 0 || count[i - 1][j] == 0 || count[i][j - 1] == 0) { | ||
count[i][j] = 1; | ||
} | ||
//if guys around are the same size, I can be the right-bottom guy of a bigger square | ||
else if(count[i - 1][j - 1] == count[i - 1][j] && count[i - 1][j] == count[i][j - 1]) { | ||
count[i][j] = count[i - 1][j - 1] + 1; | ||
} | ||
//guys around me not the same, I can only be the right-bottom guy of a least square | ||
else { | ||
count[i][j] = Math.min(Math.min(count[i - 1][j - 1], count[i - 1][j]), | ||
count[i][j - 1]) + 1; | ||
} | ||
result = Math.max(result, count[i][j]); | ||
} | ||
} | ||
return result * result; | ||
} | ||
|
||
// V3_2 | ||
// https://leetcode.com/problems/maximal-square/solutions/61828/my-java-dp-ac-solution-simple-and-easy-to-understand-with-explanation/ | ||
public int maximalSquare_3_2(char[][] matrix) { | ||
|
||
if(matrix == null || matrix.length == 0) return 0; | ||
|
||
int result = 0; | ||
int[][] count = new int[matrix.length][matrix[0].length]; | ||
|
||
for(int i = 0; i < matrix.length; i ++) { | ||
count[i][0] = matrix[i][0] == '0' ? 0 : 1; | ||
result = Math.max(result, count[i][0]); | ||
} | ||
|
||
for(int i = 0; i < matrix[0].length; i ++) { | ||
count[0][i] = matrix[0][i] == '0' ? 0 : 1; | ||
result = Math.max(result, count[0][i]); | ||
} | ||
|
||
|
||
for(int i = 1; i < matrix.length; i++) { | ||
for(int j = 1; j < matrix[0].length; j++) { | ||
|
||
if(matrix[i][j] == '0') { | ||
count[i][j] = 0; | ||
continue; | ||
} | ||
|
||
count[i][j] = Math.min(Math.min(count[i - 1][j - 1], count[i - 1][j]), | ||
count[i][j - 1]) + 1; | ||
result = Math.max(result, count[i][j]); | ||
} | ||
} | ||
return result * result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters