|
1 | 1 | package g3401_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 | | -import java.util.Arrays; |
| 4 | +// #2025_02_21_Time_51_ms_(77.23%)_Space_75.55_MB_(90.55%) |
7 | 5 |
|
8 | 6 | public class Solution { |
9 | | - private final int[][] ds = {{1, 1}, {1, -1}, {-1, -1}, {-1, 1}}; |
10 | | - private final int[] nx = {2, 2, 0}; |
11 | | - private int[][] grid; |
12 | | - private int n; |
13 | | - private int m; |
14 | | - private int[][][][] dp; |
| 7 | + private int[][] directions = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}}; |
15 | 8 |
|
16 | | - public int lenOfVDiagonal(int[][] g) { |
17 | | - this.grid = g; |
18 | | - this.n = g.length; |
19 | | - this.m = g[0].length; |
20 | | - this.dp = new int[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 | + 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]; |
| 15 | + for (int i = 0; i < m; ++i) { |
| 16 | + for (int j = 0; j < n; ++j) { |
| 17 | + bottomLeft[i][j] = 1; |
| 18 | + bottomRight[i][j] = 1; |
| 19 | + topLeft[i][j] = 1; |
| 20 | + topRight[i][j] = 1; |
26 | 21 | } |
27 | 22 | } |
28 | | - int res = 0; |
29 | | - for (int i = 0; i < n; i++) { |
30 | | - for (int j = 0; j < m; j++) { |
31 | | - if (g[i][j] == 1) { |
32 | | - for (int d = 0; d < 4; d++) { |
33 | | - res = Math.max(res, dp(i, j, 1, d, 1)); |
34 | | - } |
| 23 | + int ans = 0; |
| 24 | + for (int i = 0; i < m; ++i) { |
| 25 | + for (int j = 0; j < n; ++j) { |
| 26 | + int x = grid[i][j]; |
| 27 | + if (x == 1) { |
| 28 | + ans = 1; |
| 29 | + continue; |
| 30 | + } |
| 31 | + if (i > 0 && j + 1 < n && grid[i - 1][j + 1] == 2 - x) { |
| 32 | + bottomLeft[i][j] = bottomLeft[i - 1][j + 1] + 1; |
| 33 | + } |
| 34 | + if (i > 0 && j > 0 && grid[i - 1][j - 1] == 2 - x) { |
| 35 | + bottomRight[i][j] = bottomRight[i - 1][j - 1] + 1; |
35 | 36 | } |
36 | 37 | } |
37 | 38 | } |
38 | | - return res; |
39 | | - } |
40 | | - |
41 | | - private int dp(int i, int j, int x, int d, int k) { |
42 | | - if (i < 0 || i >= n || j < 0 || j >= m) { |
43 | | - return 0; |
44 | | - } |
45 | | - if (grid[i][j] != x) { |
46 | | - return 0; |
47 | | - } |
48 | | - if (dp[i][j][d][k] != -1) { |
49 | | - return dp[i][j][d][k]; |
| 39 | + for (int i = m - 1; i >= 0; --i) { |
| 40 | + for (int j = n - 1; j >= 0; --j) { |
| 41 | + int x = grid[i][j]; |
| 42 | + if (x == 1) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + if (i + 1 < m && j + 1 < n && grid[i + 1][j + 1] == 2 - x) { |
| 46 | + topLeft[i][j] = topLeft[i + 1][j + 1] + 1; |
| 47 | + } |
| 48 | + if (i + 1 < m && j > 0 && grid[i + 1][j - 1] == 2 - x) { |
| 49 | + topRight[i][j] = topRight[i + 1][j - 1] + 1; |
| 50 | + } |
| 51 | + } |
50 | 52 | } |
51 | | - int res = dp(i + ds[d][0], j + ds[d][1], nx[x], d, k) + 1; |
52 | | - if (k > 0) { |
53 | | - int d2 = (d + 1) % 4; |
54 | | - int res2 = dp(i + ds[d2][0], j + ds[d2][1], nx[x], d2, 0) + 1; |
55 | | - res = Math.max(res, res2); |
| 53 | + int[][][] memo = {topLeft, topRight, bottomRight, bottomLeft}; |
| 54 | + for (int i = 0; i < m; ++i) { |
| 55 | + for (int j = 0; j < n; ++j) { |
| 56 | + int x = grid[i][j]; |
| 57 | + if (x == 1) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + x >>= 1; |
| 61 | + for (int k = 0; k < 4; ++k) { |
| 62 | + int v = memo[k][i][j]; |
| 63 | + if ((v & 1) != x) { |
| 64 | + continue; |
| 65 | + } |
| 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]); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
56 | 76 | } |
57 | | - dp[i][j][d][k] = res; |
58 | | - return res; |
| 77 | + return ans; |
59 | 78 | } |
60 | 79 | } |
0 commit comments