Welcome to Subscribe On Youtube
3459. Length of Longest V-Shaped Diagonal Segment
Description
You are given a 2D integer matrix grid
of size n x m
, where each element is either 0
, 1
, or 2
.
A V-shaped diagonal segment is defined as:
- The segment starts with
1
. - The subsequent elements follow this infinite sequence:
2, 0, 2, 0, ...
. - The segment:
- Starts along a diagonal direction (top-left to bottom-right, bottom-right to top-left, top-right to bottom-left, or bottom-left to top-right).
- Continues the sequence in the same diagonal direction.
- Makes at most one clockwise 90-degree turn to another diagonal direction while maintaining the sequence.
Return the length of the longest V-shaped diagonal segment. If no valid segment exists, return 0.
Example 1:
Input: grid = [[2,2,1,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]
Output: 5
Explanation:
The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,2) → (1,3) → (2,4)
, takes a 90-degree clockwise turn at (2,4)
, and continues as (3,3) → (4,2)
.
Example 2:
Input: grid = [[2,2,2,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]
Output: 4
Explanation:
The longest V-shaped diagonal segment has a length of 4 and follows these coordinates: (2,3) → (3,2)
, takes a 90-degree clockwise turn at (3,2)
, and continues as (2,1) → (1,0)
.
Example 3:
Input: grid = [[1,2,2,2,2],[2,2,2,2,0],[2,0,0,0,0],[0,0,2,2,2],[2,0,0,2,0]]
Output: 5
Explanation:
The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,0) → (1,1) → (2,2) → (3,3) → (4,4)
.
Example 4:
Input: grid = [[1]]
Output: 1
Explanation:
The longest V-shaped diagonal segment has a length of 1 and follows these coordinates: (0,0)
.
Constraints:
n == grid.length
m == grid[i].length
1 <= n, m <= 500
grid[i][j]
is either0
,1
or2
.
Solutions
Solution 1
-
class Solution: def lenOfVDiagonal(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) next_digit = {1: 2, 2: 0, 0: 2} def within_bounds(i, j): return 0 <= i < m and 0 <= j < n @cache def f(i, j, di, dj, turned): result = 1 successor = next_digit[grid[i][j]] if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor: result = 1 + f(i + di, j + dj, di, dj, turned) if not turned: di, dj = dj, -di if within_bounds(i + di, j + dj) and grid[i + di][j + dj] == successor: result = max(result, 1 + f(i + di, j + dj, di, dj, True)) return result directions = ((1, 1), (-1, 1), (1, -1), (-1, -1)) result = 0 for i in range(m): for j in range(n): if grid[i][j] != 1: continue for di, dj in directions: result = max(result, f(i, j, di, dj, False)) return result
-
class Solution { private int m, n; private final int[] dirs = {1, 1, -1, -1, 1}; private Integer[][][][] f; public int lenOfVDiagonal(int[][] grid) { m = grid.length; n = grid[0].length; f = new Integer[m][n][4][2]; int ans = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == 1) { for (int k = 0; k < 4; k++) { ans = Math.max(ans, dfs(grid, i, j, k, 1) + 1); } } } } return ans; } private int dfs(int[][] grid, int i, int j, int k, int cnt) { if (f[i][j][k][cnt] != null) { return f[i][j][k][cnt]; } int x = i + dirs[k]; int y = j + dirs[k + 1]; int target = grid[i][j] == 1 ? 2 : (2 - grid[i][j]); if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != target) { f[i][j][k][cnt] = 0; return 0; } int res = dfs(grid, x, y, k, cnt); if (cnt > 0) { res = Math.max(res, dfs(grid, x, y, (k + 1) % 4, 0)); } f[i][j][k][cnt] = 1 + res; return 1 + res; } }
-
class Solution { public: static constexpr int MAXN = 501; int f[MAXN][MAXN][4][2]; int lenOfVDiagonal(vector<vector<int>>& grid) { int m = grid.size(), n = grid[0].size(); int dirs[5] = {1, 1, -1, -1, 1}; memset(f, -1, sizeof(f)); auto dfs = [&](this auto&& dfs, int i, int j, int k, int cnt) -> int { if (f[i][j][k][cnt] != -1) { return f[i][j][k][cnt]; } int x = i + dirs[k]; int y = j + dirs[k + 1]; int target = grid[i][j] == 1 ? 2 : (2 - grid[i][j]); if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != target) { f[i][j][k][cnt] = 0; return 0; } int res = dfs(x, y, k, cnt); if (cnt > 0) { res = max(res, dfs(x, y, (k + 1) % 4, 0)); } f[i][j][k][cnt] = 1 + res; return 1 + res; }; int ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1) { for (int k = 0; k < 4; ++k) { ans = max(ans, dfs(i, j, k, 1) + 1); } } } } return ans; } };
-
func lenOfVDiagonal(grid [][]int) int { m, n := len(grid), len(grid[0]) dirs := []int{1, 1, -1, -1, 1} f := make([][][4][2]int, m) for i := range f { f[i] = make([][4][2]int, n) } var dfs func(i, j, k, cnt int) int dfs = func(i, j, k, cnt int) int { if f[i][j][k][cnt] != 0 { return f[i][j][k][cnt] } x := i + dirs[k] y := j + dirs[k+1] var target int if grid[i][j] == 1 { target = 2 } else { target = 2 - grid[i][j] } if x < 0 || x >= m || y < 0 || y >= n || grid[x][y] != target { f[i][j][k][cnt] = 0 return 0 } res := dfs(x, y, k, cnt) if cnt > 0 { res = max(res, dfs(x, y, (k+1)%4, 0)) } f[i][j][k][cnt] = res + 1 return res + 1 } ans := 0 for i := 0; i < m; i++ { for j := 0; j < n; j++ { if grid[i][j] == 1 { for k := 0; k < 4; k++ { ans = max(ans, dfs(i, j, k, 1)+1) } } } } return ans }