|
1 | 1 | packageg3401_3500.s3459_length_of_longest_v_shaped_diagonal_segment;
|
2 | 2 |
|
3 | 3 | // #Hard #Array #Dynamic_Programming #Matrix #Memoization
|
4 |
| -// #2025_02_18_Time_461_ms_(36.09%)_Space_127.47_MB_(39.48%) |
5 |
| - |
6 |
| -importjava.util.Arrays; |
| 4 | +// #2025_02_21_Time_56_ms_(72.97%)_Space_75.44_MB_(91.21%) |
7 | 5 |
|
8 | 6 | publicclassSolution {
|
9 |
| -privatefinalint[][]ds = {{1,1}, {1, -1}, {-1, -1}, {-1,1}}; |
10 |
| -privatefinalint[]nx = {2,2,0}; |
11 |
| -privateint[][]grid; |
12 |
| -privateintn; |
13 |
| -privateintm; |
14 |
| -privateint[][][][]dp; |
| 7 | +privatefinalint[][]directions = {{-1, -1}, {-1,1}, {1,1}, {1, -1}}; |
15 | 8 |
|
16 |
| -publicintlenOfVDiagonal(int[][]g) { |
17 |
| -this.grid =g; |
18 |
| -this.n =g.length; |
19 |
| -this.m =g[0].length; |
20 |
| -this.dp =newint[n][m][4][2]; |
21 |
| -for (int[][][]d1 :dp) { |
22 |
| -for (int[][]d2 :d1) { |
23 |
| -for (int[]d3 :d2) { |
24 |
| -Arrays.fill(d3, -1); |
25 |
| - } |
| 9 | +privatevoidinitializeArrays( |
| 10 | +int[][]bottomLeft, |
| 11 | +int[][]bottomRight, |
| 12 | +int[][]topLeft, |
| 13 | +int[][]topRight, |
| 14 | +intm, |
| 15 | +intn) { |
| 16 | +for (inti =0;i <m; ++i) { |
| 17 | +for (intj =0;j <n; ++j) { |
| 18 | +bottomLeft[i][j] =1; |
| 19 | +bottomRight[i][j] =1; |
| 20 | +topLeft[i][j] =1; |
| 21 | +topRight[i][j] =1; |
26 | 22 | }
|
27 | 23 | }
|
28 |
| -intres =0; |
29 |
| -for (inti =0;i <n;i++) { |
30 |
| -for (intj =0;j <m;j++) { |
31 |
| -if (g[i][j] ==1) { |
32 |
| -for (intd =0;d <4;d++) { |
33 |
| -res =Math.max(res,dp(i,j,1,d,1)); |
34 |
| - } |
| 24 | + } |
| 25 | + |
| 26 | +privateintprocessBottomDirections( |
| 27 | +int[][]grid,int[][]bottomLeft,int[][]bottomRight,intm,intn) { |
| 28 | +intans =0; |
| 29 | +for (inti =0;i <m; ++i) { |
| 30 | +for (intj =0;j <n; ++j) { |
| 31 | +intx =grid[i][j]; |
| 32 | +if (x ==1) { |
| 33 | +ans =1; |
| 34 | +continue; |
| 35 | + } |
| 36 | +if (i >0 &&j +1 <n &&grid[i -1][j +1] ==2 -x) { |
| 37 | +bottomLeft[i][j] =bottomLeft[i -1][j +1] +1; |
| 38 | + } |
| 39 | +if (i >0 &&j >0 &&grid[i -1][j -1] ==2 -x) { |
| 40 | +bottomRight[i][j] =bottomRight[i -1][j -1] +1; |
35 | 41 | }
|
36 | 42 | }
|
37 | 43 | }
|
38 |
| -returnres; |
| 44 | +returnans; |
39 | 45 | }
|
40 | 46 |
|
41 |
| -privateintdp(inti,intj,intx,intd,intk) { |
42 |
| -if (i <0 ||i >=n ||j <0 ||j >=m) { |
43 |
| -return0; |
44 |
| - } |
45 |
| -if (grid[i][j] !=x) { |
46 |
| -return0; |
47 |
| - } |
48 |
| -if (dp[i][j][d][k] != -1) { |
49 |
| -returndp[i][j][d][k]; |
| 47 | +privatevoidprocessTopDirections( |
| 48 | +int[][]grid,int[][]topLeft,int[][]topRight,intm,intn) { |
| 49 | +for (inti =m -1;i >=0; --i) { |
| 50 | +for (intj =n -1;j >=0; --j) { |
| 51 | +intx =grid[i][j]; |
| 52 | +if (x ==1) { |
| 53 | +continue; |
| 54 | + } |
| 55 | +if (i +1 <m &&j +1 <n &&grid[i +1][j +1] ==2 -x) { |
| 56 | +topLeft[i][j] =topLeft[i +1][j +1] +1; |
| 57 | + } |
| 58 | +if (i +1 <m &&j >0 &&grid[i +1][j -1] ==2 -x) { |
| 59 | +topRight[i][j] =topRight[i +1][j -1] +1; |
| 60 | + } |
| 61 | + } |
50 | 62 | }
|
51 |
| -intres =dp(i +ds[d][0],j +ds[d][1],nx[x],d,k) +1; |
52 |
| -if (k >0) { |
53 |
| -intd2 = (d +1) %4; |
54 |
| -intres2 =dp(i +ds[d2][0],j +ds[d2][1],nx[x],d2,0) +1; |
55 |
| -res =Math.max(res,res2); |
| 63 | + } |
| 64 | + |
| 65 | +privateintfindMaxDiagonal(int[][]grid,int[][][]memo,intm,intn,intinitialAns) { |
| 66 | +intans =initialAns; |
| 67 | +for (inti =0;i <m; ++i) { |
| 68 | +for (intj =0;j <n; ++j) { |
| 69 | +intx =grid[i][j]; |
| 70 | +if (x ==1) { |
| 71 | +continue; |
| 72 | + } |
| 73 | +x >>=1; |
| 74 | +for (intk =0;k <4; ++k) { |
| 75 | +intv =memo[k][i][j]; |
| 76 | +if ((v &1) !=x) { |
| 77 | +continue; |
| 78 | + } |
| 79 | +if (v +memo[k +3 &3][i][j] >ans) { |
| 80 | +int[]d =directions[k]; |
| 81 | +intni =i -d[0] *v; |
| 82 | +intnj =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 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
56 | 89 | }
|
57 |
| -dp[i][j][d][k] =res; |
58 |
| -returnres; |
| 90 | +returnans; |
| 91 | + } |
| 92 | + |
| 93 | +publicintlenOfVDiagonal(int[][]grid) { |
| 94 | +intm =grid.length; |
| 95 | +intn =grid[0].length; |
| 96 | +int[][]bottomLeft =newint[m][n]; |
| 97 | +int[][]bottomRight =newint[m][n]; |
| 98 | +int[][]topLeft =newint[m][n]; |
| 99 | +int[][]topRight =newint[m][n]; |
| 100 | +initializeArrays(bottomLeft,bottomRight,topLeft,topRight,m,n); |
| 101 | +intans =processBottomDirections(grid,bottomLeft,bottomRight,m,n); |
| 102 | +processTopDirections(grid,topLeft,topRight,m,n); |
| 103 | +int[][][]memo = {topLeft,topRight,bottomRight,bottomLeft}; |
| 104 | +returnfindMaxDiagonal(grid,memo,m,n,ans); |
59 | 105 | }
|
60 | 106 | }
|