Skip to main content

Length of Longest V-Shaped Diagonal Segment - Video Solutions

HardArrayDynamic ProgrammingMemoizationMatrix

Length of Longest V-Shaped Diagonal Segment | Made Easy | Leetcode 3459 | codestorywithMIK

codestorywithMIK
43:367,196 views
10 video solutions available

Length of Longest V-Shaped Diagonal Segment - Video Solution

Watch 10 video solutions for Length of Longest V-Shaped Diagonal Segment, a hard level problem involving Array, Dynamic Programming, Memoization. This walkthrough by codestorywithMIK has 7,196 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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 either 0, 1 or 2.
Read full problem with examples

Approach Overview

Problem Overview: You are given a matrix and need to find the maximum length of a segment that forms a V shape along diagonals. A valid segment starts on a diagonal direction, continues for some length, then changes to another diagonal direction exactly once to form the V. The task is to scan the grid and compute the longest such path.

Approach 1: Brute Force Diagonal Exploration (O(m*n*L), O(1) space)

Start from every cell and attempt to extend a segment along all four diagonal directions. At each step, continue moving diagonally until you decide to switch to the other diagonal direction to form the V. Track the length while validating bounds and constraints. This approach repeatedly recomputes overlapping paths because the same diagonal subpaths are explored from multiple starting points. For larger grids this quickly becomes expensive since every extension may scan up to L cells along diagonals.

Approach 2: Memoized Search with Dynamic Programming (O(m*n), O(m*n) space)

The optimal approach treats the grid as a state graph and uses DFS with memoization. Each state is defined by (row, col, direction, turned) where direction is one of the four diagonals and turned indicates whether the V turn has already happened. From each state you attempt two transitions: continue in the same diagonal or switch direction if the turn has not yet been used. Store results in a memo table so every state is computed once. This removes repeated exploration and reduces the runtime to linear in the number of cells multiplied by constant directional states.

The memoized DFS behaves like classic grid dynamic programming. Each diagonal extension builds on previously solved states. Because transitions only move to neighboring cells, the recursion depth is limited by grid size and the DP cache ensures optimal substructure reuse.

This technique is similar to path problems solved using memoization on top of DFS over a matrix. The key insight is representing the "turn" as part of the state so the algorithm knows whether the V pivot has already occurred.

Recommended for interviews: Interviewers expect the memoized search solution. A brute-force exploration shows you understand the geometric constraint of the V shape, but the optimized DFS + memoization demonstrates strong dynamic programming skills and awareness of overlapping subproblems. The final solution runs in O(m*n) time with O(m*n) caching, which scales comfortably even for large matrices.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Diagonal ExplorationO(m*n*L)O(1)Useful for understanding the V-shape constraint and validating logic on small grids
DFS with Memoization (Dynamic Programming)O(m*n)O(m*n)Best general solution. Avoids recomputation of diagonal paths and scales well for large matrices