Length of Longest V-Shaped Diagonal Segment - Problem

You're navigating through a mystical grid where each cell contains either a 0 (empty space), 1 (starting point), or 2 (energy boost). Your goal is to find the longest possible V-shaped diagonal journey.

What makes a valid V-shaped diagonal segment?

  • πŸš€ Start: Must begin with a cell containing 1
  • πŸ”„ Pattern: After the starting 1, follow the infinite sequence: 2, 0, 2, 0, 2, 0...
  • ↗️ Direction: Move diagonally (NE, NW, SE, or SW)
  • πŸ”€ Turn: You can make at most one 90-degree clockwise turn to change diagonal direction

Think of it like drawing a "V" or "L" shape on the grid while collecting the right sequence of numbers!

Example: A path might go NE for 3 steps following 1β†’2β†’0β†’2, then turn clockwise to SE and continue β†’0β†’2β†’0 for a total length of 7.

Return the length of the longest valid V-shaped diagonal segment, or 0 if none exists.

Input & Output

example_1.py β€” Basic V-Shape
$ Input: grid = [[1,2,0],[0,2,1],[2,0,2]]
β€Ί Output: 4
πŸ’‘ Note: Starting at (0,0) with value 1, we can go SE to (1,1) with value 2, then SE to (2,2) with value 2 (but we expect 0, so this doesn't work). Starting at (1,2) with value 1, we can go SW to (2,1) with value 0, then SW to out of bounds. The optimal path starts at (0,0): 1β†’2β†’0β†’2 going NE then turning to SE, giving length 4.
example_2.py β€” Straight Line
$ Input: grid = [[1,2,0,2,0]]
β€Ί Output: 5
πŸ’‘ Note: A straight diagonal line starting with 1 and following the sequence 2,0,2,0... The path goes: (0,0)β†’(0,1)β†’(0,2)β†’(0,3)β†’(0,4) but since we need diagonal movement, this example would be modified to a diagonal case. For a proper diagonal: 1 at (0,0), 2 at (1,1), 0 at (2,2), 2 at (3,3), 0 at (4,4) would give length 5.
example_3.py β€” No Valid Path
$ Input: grid = [[0,2,1],[1,0,2],[2,1,0]]
β€Ί Output: 1
πŸ’‘ Note: There are starting points with value 1 at (1,0) and (2,1), but no valid continuation following the required sequence in diagonal directions. Each starting point contributes length 1 but cannot extend further, so maximum is 1.

Constraints

  • 1 ≀ n, m ≀ 100
  • grid[i][j] ∈ {0, 1, 2}
  • At least one cell contains value 1 (guaranteed starting point)
  • Diagonal directions only: NE, SE, SW, NW movements
  • At most one turn: Can change direction clockwise once

Visualization

Tap to expand
120221002V-shaped path: 1β†’2β†’0β†’21202Memoization CacheState: (row, col, dir, val, turned)Cache: Max length from this stateBenefit: O(1) lookup vs O(4^k) recalcDynamic Programming Approach🎯 Key Insight: Cache subproblem results to avoid exponential recalculation
Understanding the Visualization
1
Find Starting Crystals
Locate all cells containing '1' - these are your possible starting points
2
Choose Direction
From each start, pick one of 4 diagonal directions: NE, SE, SW, NW
3
Follow the Pattern
Move diagonally following the sequence: 1β†’2β†’0β†’2β†’0β†’2β†’0...
4
Strategic Turn
At any point, you can make one clockwise 90Β° turn to a new diagonal direction
5
Maximize Length
Continue until pattern breaks or bounds reached, tracking maximum length
Key Takeaway
🎯 Key Insight: By using memoization to cache the maximum length achievable from each state (position + direction + expected value + turn status), we transform an exponential brute force algorithm into an efficient polynomial solution.
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 31 Apple 18
52.0K Views
Medium-High Frequency
~35 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen