-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxSquareSubMatrix.java
159 lines (129 loc) · 3.76 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Problem Description
Given a 2D binary matrix A of size N x M find the area of maximum size square sub-matrix with all 1's.
Problem Constraints
1 <= N, M <= 103
A[i][j] = 1 or A[i][j] = 0
Input Format
First argument is an 2D matrix A of size N x M.
Output Format
Output the area of maximum size square sub-matrix in A with all 1's.
Example Input
Input 1:
A = [
[0, 1, 1, 0, 1],
[1, 1, 0, 1, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 0]
]
Input 2:
A = [
[1, 1],
[1, 1]
]
Example Output
Output 1:
9
Output 2:
4
Example Explanation
Explanation 1:
Consider the below binary matrix.
The area of the square is 3 * 3 = 9
Explanation 2:
The given matrix is the largest size square possible so area will be 2 * 2 = 4
*/
public class Solution {
public int solve(ArrayList<ArrayList<Integer>> A) {
int m = A.size();
int n = A.get(0).size();
if(m == 0){
return 0;
}
int[] arr = new int[n];
Arrays.fill(arr,0);
int maxArea = 0;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(A.get(i).get(j) == 0){
arr[j] = 0;
}
else{
arr[j] = arr[j] + 1;
}
}
int[] nsl = NSL(arr);
int[] nsr = NSR(arr);
for(int j=0; j<arr.length; j++){
int width = nsr[j] - nsl[j] - 1;
if(width >= arr[j]){. //if width is greater or equal to height (then square of that height is possible)
int area = arr[j] * arr[j];
maxArea = Math.max(area,maxArea);
}
}
}
return maxArea;
}
private int[] NSL(int[] arr){
int[] nsl = new int[arr.length];
Stack<Integer> st = new Stack<>();
for(int i=0; i<arr.length; i++){
if(st.isEmpty()){
nsl[i] = -1;
st.push(i);
}
else{
if(arr[i] > arr[st.peek()]){
nsl[i] = st.peek();
st.push(i);
}
else if(arr[i] <= arr[st.peek()]){
while(!st.isEmpty() && arr[i] <= arr[st.peek()]){
st.pop();
}
if(st.isEmpty()){
nsl[i] = -1;
st.push(i);
}
else{
nsl[i] = st.peek();
st.push(i);
}
}
}
}
return nsl;
}
private int[] NSR(int[] arr){
int[] nsr = new int[arr.length];
Stack<Integer> st = new Stack<>();
for(int i=arr.length-1; i>=0; i--){
if(st.isEmpty()){
nsr[i] = arr.length;
st.push(i);
}
else{
if(arr[i] > arr[st.peek()]){
nsr[i] = st.peek();
st.push(i);
}
else if(arr[i] <= arr[st.peek()]){
while(!st.isEmpty() && arr[i] <= arr[st.peek()]){
st.pop();
}
if(st.isEmpty()){
nsr[i] = arr.length;
st.push(i);
}
else{
nsr[i] = st.peek();
st.push(i);
}
}
}
}
return nsr;
}
}