From af981f15f44d7594cdce362e3c734955013c29f2 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Thu, 3 Oct 2024 15:56:28 +0800 Subject: [PATCH] add 221 java, progress --- README.md | 2 +- data/progress.txt | 2 +- data/to_review.txt | 18 +- .../DynamicProgramming/MaximalSquare.java | 179 ++++++++++++++++++ .../src/main/java/dev/workspace5.java | 36 ++++ 5 files changed, 226 insertions(+), 11 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MaximalSquare.java diff --git a/README.md b/README.md index 9629152f..f8684d1d 100644 --- a/README.md +++ b/README.md @@ -1039,7 +1039,7 @@ 152| [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)|[Python](./leetcode_python/Dynamic_Programming/maximum-product-subarray.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MaximumProductSubarray.java)| _O(n)_ | _O(1)_ | Medium |dp, brute force, Curated Top 75, google, amazon, linkedin| AGAIN***** (1) 198| [House Robber](https://leetcode.com/problems/house-robber/)| [Python](./leetcode_python/Dynamic_Programming/house-robber.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber.java) | _O(n)_ | _O(1)_ | Easy|Curated Top 75, dp basic,`amazon`| AGAIN*** (2) 213| [House Robber II](https://leetcode.com/problems/house-robber-ii/)| [Python](./leetcode_python/Dynamic_Programming/house-robber-ii.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/HouseRobber2.java) | _O(n)_| _O(1)_| Medium|brute force, recursion, dp, Curated Top 75, google, amazon| OK**** (3) (but again) -221| [Maximal Square](https://leetcode.com/problems/maximal-square/)|[Python](./leetcode_python/Dynamic_Programming/maximal-square.py) | _O(n^2)_ | _O(n)_ | Medium | EPI, dp, `amazon`,`fb` | AGAIN** (3) (not start) +221| [Maximal Square](https://leetcode.com/problems/maximal-square/)|[Python](./leetcode_python/Dynamic_Programming/maximal-square.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MaximalSquare.java) | _O(n^2)_ | _O(n)_ | Medium | EPI, dp, `amazon`,`fb`, google| AGAIN** (3) (not start) 256| [Paint House](https://leetcode.com/problems/paint-house/) | [Python](./leetcode_python/Dynamic_Programming/paint-house.py) | _O(n)_| _O(1)_| Medium |🔒| AGAIN (not start) 276| [Paint Fence](https://leetcode.com/problems/paint-fence/) | [Python](./leetcode_python/Dynamic_Programming/paint-fence.py) | _O(n)_| _O(1)_| Easy |🔒| AGAIN 279| [Perfect Squares](https://leetcode.com/problems/perfect-squares/)| [Python](./leetcode_python/Dynamic_Programming/perfect-squares.py) | _O(n * sqrt(n))_ | _O(n)_ | Medium | dp, bfs, trick, Hash, `google` | AGAIN* diff --git a/data/progress.txt b/data/progress.txt index c94b420c..86d59046 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20241003: 524 +20241003: 524,221 20241002: 743,889 20241001: 837 20240929: 981 diff --git a/data/to_review.txt b/data/to_review.txt index 934ed9cc..60584e0d 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2024-11-27 -> ['524'] +2024-11-27 -> ['524,221'] 2024-11-26 -> ['743,889'] 2024-11-25 -> ['837'] 2024-11-23 -> ['981'] @@ -10,7 +10,7 @@ 2024-11-15 -> ['947'] 2024-11-12 -> ['753'] 2024-11-11 -> ['727'] -2024-11-06 -> ['524'] +2024-11-06 -> ['524,221'] 2024-11-05 -> ['743,889'] 2024-11-04 -> ['837', '659'] 2024-11-03 -> ['801,552'] @@ -21,7 +21,7 @@ 2024-10-27 -> ['363'] 2024-10-26 -> ['1032,844,1011'] 2024-10-25 -> ['947', '1110, 1055'] -2024-10-24 -> ['524'] +2024-10-24 -> ['524,221'] 2024-10-23 -> ['743,889'] 2024-10-22 -> ['837', '753'] 2024-10-21 -> ['727'] @@ -29,19 +29,19 @@ 2024-10-19 -> ['1087'] 2024-10-18 -> ['359,1057,1055(todo)'] 2024-10-17 -> ['939'] -2024-10-16 -> ['524'] +2024-10-16 -> ['524,221'] 2024-10-15 -> ['743,889', '430'] 2024-10-14 -> ['837', '363', '659'] 2024-10-13 -> ['1032,844,1011', '801,552'] 2024-10-12 -> ['981', '947', '1057,1066,1110'] -2024-10-11 -> ['524', '1087'] +2024-10-11 -> ['524,221', '1087'] 2024-10-10 -> ['743,889'] 2024-10-09 -> ['837', '939', '753'] -2024-10-08 -> ['524', '727'] +2024-10-08 -> ['524,221', '727'] 2024-10-07 -> ['743,889', '981', '430'] -2024-10-06 -> ['524', '837', '1087', '363'] -2024-10-05 -> ['524', '743,889', '1032,844,1011'] -2024-10-04 -> ['524', '743,889', '837', '981', '939', '947', '315', '1110, 1055'] +2024-10-06 -> ['524,221', '837', '1087', '363'] +2024-10-05 -> ['524,221', '743,889', '1032,844,1011'] +2024-10-04 -> ['524,221', '743,889', '837', '981', '939', '947', '315', '1110, 1055'] 2024-10-03 -> ['743,889', '837', '1087'] 2024-10-02 -> ['837', '981', '430'] 2024-10-01 -> ['981', '1087', '939', '363', '753', '659'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MaximalSquare.java b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MaximalSquare.java new file mode 100644 index 00000000..49d186b8 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/DynamicProgramming/MaximalSquare.java @@ -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; + } + +} diff --git a/leetcode_java/src/main/java/dev/workspace5.java b/leetcode_java/src/main/java/dev/workspace5.java index 08e66cca..4808aa12 100644 --- a/leetcode_java/src/main/java/dev/workspace5.java +++ b/leetcode_java/src/main/java/dev/workspace5.java @@ -848,4 +848,40 @@ private boolean canForm(String x, String y){ return j == x.length(); } + // LC 221 + // https://leetcode.com/problems/maximal-square/ + // 3.38 pm - 3.48 + /** + * find the largest square containing only 1's and return its area. + * + * + * idea 1) + * + * 1 + * + * 11 + * 1? + * + * 111 + * 111 + * 11? + * + * + * dp[0][0] = 1 or 0 + * + * dp[i][j] = if (dp[i-1]dp[j]==1 && dp[i]dp[j-1]==1), then 1 + * else 0 + * + * ... + * + */ + public int maximalSquare(char[][] matrix) { + if (matrix.length == 1 && matrix[0].length == 1){ + return matrix[0][0]; + } + + int res = 0; + return 0; + } + }