Skip to content
Discussion options

You must be logged in to vote

We need to find the longest V-shaped diagonal segment in a 2D grid where the segment starts with 1, followed by the sequence 2, 0, 2, 0, etc., and can make at most one clockwise 90-degree turn. The solution involves using dynamic programming to precompute the lengths of valid segments in all four diagonal directions both with and without turns.

Approach

  1. Direction Definitions: Define the four diagonal directions as (1,1), (1,-1), (-1,-1), and (-1,1).
  2. Dynamic Programming Arrays: Initialize two 3D arrays, dp and rev_dp, of dimensions 4 x n x m. The dp array stores the lengths of segments starting from each cell in each direction without any turns. The rev_dp array stores the lengths of segm…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Aug 27, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested hard Difficulty
2 participants