44// #2025_02_21_Time_51_ms_(77.23%)_Space_75.55_MB_(90.55%)
55
66public 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 }};
88
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 ) {
1516 for (int i = 0 ; i < m ; ++i ) {
1617 for (int j = 0 ; j < n ; ++j ) {
1718 bottomLeft [i ][j ] = 1 ;
@@ -20,6 +21,10 @@ public int lenOfVDiagonal(int[][] grid) {
2021 topRight [i ][j ] = 1 ;
2122 }
2223 }
24+ }
25+
26+ private int processBottomDirections (
27+ int [][] grid , int [][] bottomLeft , int [][] bottomRight , int m , int n ) {
2328 int ans = 0 ;
2429 for (int i = 0 ; i < m ; ++i ) {
2530 for (int j = 0 ; j < n ; ++j ) {
@@ -36,6 +41,11 @@ public int lenOfVDiagonal(int[][] grid) {
3641 }
3742 }
3843 }
44+ return ans ;
45+ }
46+
47+ private void processTopDirections (
48+ int [][] grid , int [][] topLeft , int [][] topRight , int m , int n ) {
3949 for (int i = m - 1 ; i >= 0 ; --i ) {
4050 for (int j = n - 1 ; j >= 0 ; --j ) {
4151 int x = grid [i ][j ];
@@ -50,7 +60,10 @@ public int lenOfVDiagonal(int[][] grid) {
5060 }
5161 }
5262 }
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 ;
5467 for (int i = 0 ; i < m ; ++i ) {
5568 for (int j = 0 ; j < n ; ++j ) {
5669 int x = grid [i ][j ];
@@ -63,17 +76,31 @@ public int lenOfVDiagonal(int[][] grid) {
6376 if ((v & 1 ) != x ) {
6477 continue ;
6578 }
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+ }
7386 }
7487 }
7588 }
7689 }
7790 return ans ;
7891 }
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+ }
79106}
0 commit comments