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: Memoized Search
We design a function \(\text{dfs}(i, j, k, \textit{cnt})\) that returns the length of the longest V-shaped diagonal segment, where \((i, j)\) is the previous position, \(k\) is the current direction, and \(\textit{cnt}\) is the remaining number of allowed turns.
The logic of the \(\text{dfs}\) function is as follows:
First, based on the previous position and the current direction, we calculate the current position \((x, y)\) and determine the target value \(\textit{target}\). If \(x\) or \(y\) is out of bounds, or if \(\textit{grid}[x][y] \neq \textit{target}\), we return \(0\).
Otherwise, we have two choices:
Continue moving in the current direction. Make a 90-degree clockwise turn at the current position and then continue moving. To implement the turn, if the current direction is \(k\), the new direction after a 90-degree clockwise turn is \((k + 1) \bmod 4\). We take the maximum result from these two choices as the result for the current state.
In the main function, we iterate over the entire matrix. For each position with value 1, we try searching in all four directions and update the answer.
After the traversal, we return the answer.
To avoid repeated calculations, we use memoization to cache intermediate results.
The time complexity is \(O(m \times n)\), and the space complexity is \(O(m \times n)\), where \(m\) and \(n\) are the number of rows and columns in the matrix, respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|