Skip to content

Practice Length of Longest V-Shaped Diagonal Segment

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3459
LeetCode Problem

Length of Longest V-Shaped Diagonal Segment

Problem Statement

Given grid with values 0, 1, and 2, a valid diagonal segment starts with 1 and then follows 2, 0, 2, 0, and so on. It begins in any of the four diagonal directions and may make at most one clockwise 90-degree turn to another diagonal direction, continuing the same value sequence. Return the longest valid segment length, or 0 when no segment exists.

Source: Length of Longest V-Shaped Diagonal Segment on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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

# 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

# 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

# Example 4

Input
grid = [[1]]
Output
1

Constraints

n == grid.length
m == grid[i].length
1 <= n, m <= 500
grid[i][j] is either 0, 1 or 2.

Related LeetCode Problems

Practice more problems similar to Length of Longest V-Shaped Diagonal Segment.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.