3459. Length of Longest V-Shaped Diagonal Segment Hard

@problem@discussion
#Array#Dynamic Programming#Memoization#Matrix



1/**
2 * [3459] Length of Longest V-Shaped Diagonal Segment
3 *
4 * You are given a 2D integer matrix grid of size n x m, where each element is either 0, 1, or 2.
5 * A V-shaped diagonal segment is defined as:
6 * 
7 * 	The segment starts with 1.
8 * 	The subsequent elements follow this infinite sequence: 2, 0, 2, 0, ....
9 * 	The segment:
10 * 	
11 * 		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).
12 * 		Continues the sequence in the same diagonal direction.
13 * 		Makes at most one clockwise 90-degree turn to another diagonal direction while maintaining the sequence.
14 * 	
15 * 	
16 * 
17 * Return the length of the longest V-shaped diagonal segment. If no valid segment exists, return 0.
18 *  
19 * <strong class="example">Example 1:
20 * <div class="example-block">
21 * Input: <span class="example-io">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]]</span>
22 * Output: <span class="example-io">5</span>
23 * Explanation:
24 * <img alt="" src="https://assets.leetcode.com/uploads/2024/12/09/matrix_1-2.jpg" style="width: 201px; height: 192px;" />
25 * The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,2) &rarr; (1,3) &rarr; (2,4), takes a 90-degree clockwise turn at (2,4), and continues as (3,3) &rarr; (4,2).
26 * </div>
27 * <strong class="example">Example 2:
28 * <div class="example-block">
29 * Input: <span class="example-io">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]]</span>
30 * Output: <span class="example-io">4</span>
31 * Explanation:
32 * <img alt="" src="https://assets.leetcode.com/uploads/2024/12/09/matrix_2.jpg" style="width: 201px; height: 201px;" />
33 * The longest V-shaped diagonal segment has a length of 4 and follows these coordinates: (2,3) &rarr; (3,2), takes a 90-degree clockwise turn at (3,2), and continues as (2,1) &rarr; (1,0).
34 * </div>
35 * <strong class="example">Example 3:
36 * <div class="example-block">
37 * Input: <span class="example-io">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]]</span>
38 * Output: <span class="example-io">5</span>
39 * Explanation:
40 * <img alt="" src="https://assets.leetcode.com/uploads/2024/12/09/matrix_3.jpg" style="width: 201px; height: 201px;" />
41 * The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,0) &rarr; (1,1) &rarr; (2,2) &rarr; (3,3) &rarr; (4,4).
42 * </div>
43 * <strong class="example">Example 4:
44 * <div class="example-block">
45 * Input: <span class="example-io">grid = [[1]]</span>
46 * Output: <span class="example-io">1</span>
47 * Explanation:
48 * The longest V-shaped diagonal segment has a length of 1 and follows these coordinates: (0,0).
49 * </div>
50 *  
51 * Constraints:
52 * 
53 * 	n == grid.length
54 * 	m == grid[i].length
55 * 	1 <= n, m <= 500
56 * 	grid[i][j] is either 0, 1 or 2.
57 * 
58 */
59pub struct Solution {}
60
61// problem: https://leetcode.com/problems/length-of-longest-v-shaped-diagonal-segment/
62// discuss: https://leetcode.com/problems/length-of-longest-v-shaped-diagonal-segment/discuss/?currentPage=1&orderBy=most_votes&query=
63
64// submission codes start here
65
66impl Solution {
67    pub fn len_of_v_diagonal(grid: Vec<Vec<i32>>) -> i32 {
68        0
69    }
70}
71
72// submission codes end
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_3459() {
80    }
81}
82

Back
© 2026 bowen.ge All Rights Reserved.