-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximalSquare.java
57 lines (51 loc) · 1.47 KB
/
MaximalSquare.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
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 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Return 4.
*/
import java.io.*;
import java.util.*;
public class MaximalSquare {
public static int maximalSquare(char[][] matrix) {
if(matrix.length <= 0 || matrix[0].length <= 0) return 0;
int res = 0;
int M = matrix.length;
int N = matrix[0].length;
int[][] dp = new int[M+1][N+1];
for(int i = 1; i <= matrix.length; i++){
for(int j = 1; j <= matrix[0].length; j++){
if(matrix[i-1][j-1] == '1'){
dp[i][j] = Math.min(Math.min(dp[i-1][j-1], dp[i-1][j]), dp[i][j-1])+1;
res = Math.max(dp[i][j], res);
}
}
}
return res*res;
}
public static void dispArray2D(char[][] res){
for(int i = 0; i < res.length; i++){
for(int j = 0; j < res[i].length; j++){
System.out.print(res[i][j] + " "); // note: res[i][j]
}
System.out.println();
}
}
public static void main(String[] args) {
char[][] matrix = new char[][]{
{'0', '1', '1', '1', '1'},
{'0', '1', '1', '1', '1'},
{'1', '0', '1', '1', '1'},
{'1', '1', '1', '1', '1'},
{'0', '0', '0', '1', '1'}
};
System.out.println("Maximal square: ");
dispArray2D(matrix);
int r = maximalSquare(matrix);
System.out.println("results: " + r);
return;
}
}