Skip to content

Commit cbd3a81

Browse files
refactor 221
1 parent 80a79f6 commit cbd3a81

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

src/main/java/com/fishercoder/solutions/_221.java

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,33 @@
1515
*/
1616
public class _221 {
1717

18-
/**The idea is pretty straightforward: use a 2d dp table to store the intermediate results*/
19-
public static int maximalSquare(char[][] matrix) {
20-
if (matrix == null || matrix.length == 0) {
21-
return 0;
22-
}
23-
int m = matrix.length;
24-
int n = matrix[0].length;
25-
int max = Integer.MIN_VALUE;
26-
int[][] dp = new int[m][n];
27-
for (int i = 0; i < m; i++) {
28-
for (int j = 0; j < n; j++) {
29-
if (i == 0 || j == 0) {
30-
dp[i][j] = (matrix[i][j] == '1') ? 1 : 0;
31-
} else {
32-
if (matrix[i][j] == '0') {
33-
dp[i][j] = 0;
18+
public static class Solution1 {
19+
/**
20+
* The idea is pretty straightforward: use a 2d dp table to store the intermediate results
21+
*/
22+
public int maximalSquare(char[][] matrix) {
23+
if (matrix == null || matrix.length == 0) {
24+
return 0;
25+
}
26+
int m = matrix.length;
27+
int n = matrix[0].length;
28+
int max = Integer.MIN_VALUE;
29+
int[][] dp = new int[m][n];
30+
for (int i = 0; i < m; i++) {
31+
for (int j = 0; j < n; j++) {
32+
if (i == 0 || j == 0) {
33+
dp[i][j] = (matrix[i][j] == '1') ? 1 : 0;
3434
} else {
35-
dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
35+
if (matrix[i][j] == '0') {
36+
dp[i][j] = 0;
37+
} else {
38+
dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;
39+
}
3640
}
41+
max = (max < dp[i][j]) ? dp[i][j] : max;
3742
}
38-
max = (max < dp[i][j]) ? dp[i][j] : max;
3943
}
44+
return max * max;
4045
}
41-
return max * max;
42-
}
43-
44-
public static void main(String... strings) {
45-
char[][] matrix = new char[][]{
46-
{'1', '0', '1', '0', '0'},
47-
{'1', '0', '1', '1', '1'},
48-
{'1', '1', '1', '1', '1'},
49-
{'1', '0', '0', '1', '0'},
50-
};
51-
System.out.println(maximalSquare(matrix));
5246
}
5347
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._221;
4+
import com.fishercoder.solutions._50;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _221Test {
11+
private static _221.Solution1 solution1;
12+
private static char[][] matrix;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _221.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
matrix = new char[][]{
22+
{'1', '0', '1', '0', '0'},
23+
{'1', '0', '1', '1', '1'},
24+
{'1', '1', '1', '1', '1'},
25+
{'1', '0', '0', '1', '0'},
26+
};
27+
28+
assertEquals(4, solution1.maximalSquare(matrix));
29+
}
30+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy