#3459

Length of Longest V-Shaped Diagonal Segment

expert · 1280 · lc hard +32 · 56.3% accepted · 374 likes · top 50%

Description

You have an n x m integer matrix grid where every cell holds 0, 1, or 2.

A V-shaped diagonal segment must:

- Begin with the value 1.

- Continue with the repeating pattern 2, 0, 2, 0, ....

- Travel diagonally (any of the four diagonal directions), and make at most one clockwise 90-degree turn while preserving the value sequence.

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

Code

1
2
3