-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubSquareSurroundedByX.java
More file actions
52 lines (43 loc) · 1.47 KB
/
SubSquareSurroundedByX.java
File metadata and controls
52 lines (43 loc) · 1.47 KB
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
package ArraysD2;
public class SubSquareSurroundedByX {
static int largestSubSquare(int n, char[][] a) {
int[][] top = new int[n][n];
int[][] left = new int[n][n];
// Top metric.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 'X') {
if (i != 0) top[i][j] = top[i - 1][j] + 1;
else top[i][j] = 1;
}
}
}
// Left metric.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 'X') {
if (j != 0) left[i][j] = left[i][j - 1] + 1;
else left[i][j] = 1;
}
}
}
int maxSubSq = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (top[i][j] == 0 || left[i][j] == 0)
continue;
int currentValue = Math.min(top[i][j], left[i][j]);
while (currentValue > 0) {
int top1 = i - currentValue + 1;
int left1 = j - currentValue + 1;
if ((left[top1][j] >= currentValue) && (top[i][left1] >= currentValue)) {
maxSubSq = Math.max(maxSubSq, currentValue);
break;
}
currentValue--;
}
}
}
return maxSubSq;
}
}