-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximumSubSquareMatrix.java
More file actions
34 lines (31 loc) · 978 Bytes
/
Copy pathMaximumSubSquareMatrix.java
File metadata and controls
34 lines (31 loc) · 978 Bytes
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
public class MaximumSubSquareMatrix {
public static int solve(int arr[][]) {
int dp[][] = new int[arr.length+1][arr[0].length+1];
for (int i = 1; i < dp.length ; i++) {
for (int j = 1; j < dp[0].length; j++) {
if (arr[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;
}
}
}
return dp[arr.length][arr[0].length];
}
public static void main(String[] args) {
// Sample Input:
//
// 0 0 1 1 1
// 1 0 1 1 1
// 0 1 1 1 1
// 1 0 1 1 1
int arr[][] = {
{0, 0, 1, 1, 1},
{1, 0, 1, 1, 1},
{0, 1, 1, 1, 1},
{1, 0, 1, 1, 1},
};
int l = solve(arr);
System.out.println("Maximum Sub Square Matrix is " + l*l);
// Output:
// Maximum Sub Square Matrix is 9
}
}