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
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.
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code