-
Notifications
You must be signed in to change notification settings - Fork 308
/
MaxSquareSubmatrix.java
119 lines (87 loc) · 2.91 KB
/
MaxSquareSubmatrix.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//Given a binary matrix, find out the maximum size square sub-matrix with all 1s. This is a DP problem.
// Author - Aastha Aneja (Github handle - Aashu24)
// Email - [email protected]
import java.util.Scanner;
public class MaxSquareSubmatrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter size of matrix:");
// rows
int n = in.nextInt();
// matrix of size n*m
int[][] matrix = new int[n][n];
System.out.println("Enter values of matrix:");
// matrix input
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
matrix[i][j] = in.nextInt();
}
}
// required function call
maxSquareSubMatrix(matrix);
}
public static void maxSquareSubMatrix(int[][] matrix) {
// storage - it will store maximum size of square submatrix with all 1's that
// starts at row r and column c in strg[r][c]
int[][] strg = new int[matrix.length][matrix.length];
// destination row
int dr = strg.length - 1;
// destination column
int dc = strg[0].length - 1;
// overall maximum
int omax = 0;
int omaxRow = -1;
int omaxCol = -1;
// now we iterate over the matrix
// row loop
for (int r = dr; r >= 0; --r) {
// column loop
for (int c = dc; c >= 0; --c) {
// row + 1
int rp1 = r + 1;
// column + 1
int cp1 = c + 1;
// if we are in last row or last column, largest square submatrix that can be
// formed will be of size 0 if the element at that position is 0 andof size 1 if
// the element is 1
if (r == dr || c == dc) {
// so we put the element in strg
strg[r][c] = matrix[r][c];
}
// for the rest of the matrix
else {
// if element is 0
if (matrix[r][c] == 0) {
// max size square submatrix of all 1's starting at that index is also of size 0
strg[r][c] = 0;
}
// if element is 1
else {
// we find out min of strg[rp1][cp1], strg[rp1][c] and strg[r][cp1] and add 1 to
// it as current element is 1
strg[r][c] = 1 + Math.min(strg[rp1][cp1], Math.min(strg[rp1][c], strg[r][cp1]));
}
}
// if we have found a new square submatrix of size>omax, we update omax
if (strg[r][c] > omax) {
omax = strg[r][c];
omaxRow = r;
omaxCol = c;
}
}
}
// finally, we print the ans
System.out.println("Size of maximum square submatrix : " + omax);
System.out.println("Starting row : " + omaxRow);
System.out.println("Starting column : " + omaxCol);
}
}
// Time complexity analysis -
// The time complexity of the function will be O(n^2) where n is the size of the
// square matrix as we have to fill the whole 2d matrix for strg to find biggest
// square submatrix of all 1's starting
// at every position.
// Space complexity analysis -
// The extra space used by this algorithm is O(n^2) where n is the size of the
// square matrix as we made one 2d matrix strg.