1
+ // Runtime: 80 ms (Top 59.69%) | Memory: 74.3 MB (Top 37.60%)
1
2
class Solution {
2
3
public int orderOfLargestPlusSign (int n , int [][] mines ) {
3
- // Create the matrix
4
+ // Create the matrix
4
5
int [][] arr = new int [n ][n ];
5
6
for (int [] subArray : arr ) {
6
7
Arrays .fill (subArray , 1 );
7
8
}
8
-
9
+
9
10
for (int i = 0 ; i < mines .length ; i ++) {
10
11
arr [mines [i ][0 ]][mines [i ][1 ]] = 0 ;
11
12
}
12
-
13
- // Prefix Count DP arrays
13
+
14
+ // Prefix Count DP arrays
14
15
int [][] dpTop = new int [arr .length ][arr .length ];
15
16
int [][] dpLeft = new int [arr .length ][arr .length ];
16
17
int [][] dpRight = new int [arr .length ][arr .length ];
17
18
int [][] dpBottom = new int [arr .length ][arr .length ];
18
-
19
- // Prefix count of 1 cells on top and left directions
19
+
20
+ // Prefix count of 1 cells on top and left directions
20
21
for (int i = 0 ; i < arr .length ; i ++) {
21
22
for (int j = 0 ; j < arr .length ; j ++) {
22
23
if (i - 1 >= 0 && arr [i - 1 ][j ] == 1 ) {
23
24
dpTop [i ][j ] = 1 + dpTop [i - 1 ][j ];
24
25
}
25
-
26
+
26
27
if (j - 1 >= 0 && arr [i ][j - 1 ] == 1 ) {
27
28
dpLeft [i ][j ] = 1 + dpLeft [i ][j - 1 ];
28
29
}
29
30
}
30
31
}
31
-
32
- // Prefix count of 1 cells on bottom and right directions
32
+
33
+ // Prefix count of 1 cells on bottom and right directions
33
34
for (int i = arr .length - 1 ; i >= 0 ; i --) {
34
35
for (int j = arr .length - 1 ; j >= 0 ; j --) {
35
36
if (i + 1 < arr .length && arr [i + 1 ][j ] == 1 ) {
36
37
dpBottom [i ][j ] = 1 + dpBottom [i + 1 ][j ];
37
38
}
38
-
39
+
39
40
if (j + 1 < arr .length && arr [i ][j + 1 ] == 1 ) {
40
41
dpRight [i ][j ] = 1 + dpRight [i ][j + 1 ];
41
42
}
42
43
}
43
44
}
44
-
45
+
45
46
int maxPlusSignLength = 0 ;
46
47
for (int i = 0 ; i < arr .length ; i ++) {
47
48
for (int j = 0 ; j < arr .length ; j ++) {
48
49
if (arr [i ][j ] == 0 ) continue ;
49
-
50
- // Minimum adjacent 1 cell count from all four directions to ensure symmetry
50
+
51
+ // Minimum adjacent 1 cell count from all four directions to ensure symmetry
51
52
int minAdjacentOnes = Math .min (Math .min (dpTop [i ][j ], dpBottom [i ][j ]), Math .min (dpLeft [i ][j ], dpRight [i ][j ]));
52
53
maxPlusSignLength = Math .max (maxPlusSignLength , minAdjacentOnes + 1 );
53
54
}
54
55
}
55
-
56
+
56
57
return maxPlusSignLength ;
57
58
}
58
- }
59
+ }
0 commit comments