Length of Longest V-Shaped Diagonal Segment Problem
Description
LeetCode Problem 3459.
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.lengthm == grid[i].length1 <= n, m <= 500grid[i][j]is either0,1or2.
Solution
Approach: Memoized DFS
A V-shaped segment starts with a 1, then follows the values 2, 0, 2, 0, ... along one diagonal direction, and may make one clockwise 90-degree turn and continue in the new direction.
Define dfs(i, j, k, cnt) as how many more cells we can add after cell (i, j), moving in direction k (one of four diagonals) with cnt turns still available (1 or 0). The next cell (x, y) must be inside the grid and hold the expected value: 2 after a 1, and alternately 0 and 2 after that. If it does not, the result is 0. Otherwise the result is 1 plus the better of two options: keep going in direction k, or, if a turn is still available, turn to direction (k + 1) % 4 and continue with no turns left.
From every cell holding 1 and each of the four directions, call dfs with one turn available and add 1 for the starting cell; the answer is the maximum. Memoizing on (i, j, k, cnt) makes each state computed once.
Complexity: O(mn) time and O(mn) space (there are 8mn states).
from functools import lru_cache
class Solution:
def lenOfVDiagonal(self, grid: List[List[int]]) -> int:
@lru_cache(None)
def dfs(i: int, j: int, k: int, cnt: int) -> int:
x, y = i + dirs[k], j + dirs[k + 1]
target = 2 if grid[i][j] == 1 else (2 - grid[i][j])
if not 0 <= x < m or not 0 <= y < n or grid[x][y] != target:
return 0
res = dfs(x, y, k, cnt)
if cnt > 0:
res = max(res, dfs(x, y, (k + 1) % 4, 0))
return 1 + res
m, n = len(grid), len(grid[0])
dirs = (1, 1, -1, -1, 1)
ans = 0
for i, row in enumerate(grid):
for j, x in enumerate(row):
if x == 1:
for k in range(4):
ans = max(ans, dfs(i, j, k, 1) + 1)
return ans