4
4
// #2025_02_21_Time_51_ms_(77.23%)_Space_75.55_MB_(90.55%)
5
5
6
6
public class Solution {
7
- private int [][] directions = {{-1 , -1 }, {-1 , 1 }, {1 , 1 }, {1 , -1 }};
7
+ private final int [][] directions = {{-1 , -1 }, {-1 , 1 }, {1 , 1 }, {1 , -1 }};
8
8
9
- public int lenOfVDiagonal (int [][] grid ) {
10
- int m = grid .length , n = grid [0 ].length ;
11
- int [][] bottomLeft = new int [m ][n ];
12
- int [][] bottomRight = new int [m ][n ];
13
- int [][] topLeft = new int [m ][n ];
14
- int [][] topRight = new int [m ][n ];
9
+ private void initializeArrays (
10
+ int [][] bottomLeft ,
11
+ int [][] bottomRight ,
12
+ int [][] topLeft ,
13
+ int [][] topRight ,
14
+ int m ,
15
+ int n ) {
15
16
for (int i = 0 ; i < m ; ++i ) {
16
17
for (int j = 0 ; j < n ; ++j ) {
17
18
bottomLeft [i ][j ] = 1 ;
@@ -20,6 +21,10 @@ public int lenOfVDiagonal(int[][] grid) {
20
21
topRight [i ][j ] = 1 ;
21
22
}
22
23
}
24
+ }
25
+
26
+ private int processBottomDirections (
27
+ int [][] grid , int [][] bottomLeft , int [][] bottomRight , int m , int n ) {
23
28
int ans = 0 ;
24
29
for (int i = 0 ; i < m ; ++i ) {
25
30
for (int j = 0 ; j < n ; ++j ) {
@@ -36,6 +41,11 @@ public int lenOfVDiagonal(int[][] grid) {
36
41
}
37
42
}
38
43
}
44
+ return ans ;
45
+ }
46
+
47
+ private void processTopDirections (
48
+ int [][] grid , int [][] topLeft , int [][] topRight , int m , int n ) {
39
49
for (int i = m - 1 ; i >= 0 ; --i ) {
40
50
for (int j = n - 1 ; j >= 0 ; --j ) {
41
51
int x = grid [i ][j ];
@@ -50,7 +60,10 @@ public int lenOfVDiagonal(int[][] grid) {
50
60
}
51
61
}
52
62
}
53
- int [][][] memo = {topLeft , topRight , bottomRight , bottomLeft };
63
+ }
64
+
65
+ private int findMaxDiagonal (int [][] grid , int [][][] memo , int m , int n , int initialAns ) {
66
+ int ans = initialAns ;
54
67
for (int i = 0 ; i < m ; ++i ) {
55
68
for (int j = 0 ; j < n ; ++j ) {
56
69
int x = grid [i ][j ];
@@ -63,17 +76,31 @@ public int lenOfVDiagonal(int[][] grid) {
63
76
if ((v & 1 ) != x ) {
64
77
continue ;
65
78
}
66
- if (v + memo [k + 3 & 3 ][i ][j ] <= ans ) {
67
- continue ;
68
- }
69
- int [] d = directions [ k ] ;
70
- int ni = i - d [ 0 ] * v , nj = j - d [ 1 ] * v ;
71
- if ( ni >= 0 && nj >= 0 && ni < m && nj < n && grid [ ni ][ nj ] == 1 ) {
72
- ans = Math . max ( ans , v + memo [ k + 3 & 3 ][ i ][ j ]);
79
+ if (v + memo [k + 3 & 3 ][i ][j ] > ans ) {
80
+ int [] d = directions [ k ] ;
81
+ int ni = i - d [ 0 ] * v ;
82
+ int nj = j - d [ 1 ] * v ;
83
+ if ( ni >= 0 && nj >= 0 && ni < m && nj < n && grid [ ni ][ nj ] == 1 ) {
84
+ ans = Math . max ( ans , v + memo [ k + 3 & 3 ][ i ][ j ]);
85
+ }
73
86
}
74
87
}
75
88
}
76
89
}
77
90
return ans ;
78
91
}
92
+
93
+ public int lenOfVDiagonal (int [][] grid ) {
94
+ int m = grid .length ;
95
+ int n = grid [0 ].length ;
96
+ int [][] bottomLeft = new int [m ][n ];
97
+ int [][] bottomRight = new int [m ][n ];
98
+ int [][] topLeft = new int [m ][n ];
99
+ int [][] topRight = new int [m ][n ];
100
+ initializeArrays (bottomLeft , bottomRight , topLeft , topRight , m , n );
101
+ int ans = processBottomDirections (grid , bottomLeft , bottomRight , m , n );
102
+ processTopDirections (grid , topLeft , topRight , m , n );
103
+ int [][][] memo = {topLeft , topRight , bottomRight , bottomLeft };
104
+ return findMaxDiagonal (grid , memo , m , n , ans );
105
+ }
79
106
}
0 commit comments