Skip to content

Commit a4e51fe

Browse files
committed
Add solution and test-cases for problem 3459
1 parent df4288a commit a4e51fe

File tree

7 files changed

+119
-22
lines changed

7 files changed

+119
-22
lines changed
7.75 KB
Loading
7.08 KB
Loading
7.11 KB
Loading

leetcode/3401-3500/3459.Length-of-Longest-V-Shaped-Diagonal-Segment/README.md

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,75 @@
11
# [3459.Length of Longest V-Shaped Diagonal Segment][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a 2D integer matrix `grid` of size `n x m`, where each element is either `0`, `1`, or `2`.
5+
6+
A **V-shaped diagonal segment** is defined as:
7+
8+
- The segment starts with `1`.
9+
- The subsequent elements follow this infinite sequence: `2, 0, 2, 0, ...`.
10+
- The segment:
11+
12+
- 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).
13+
- Continues the **sequence** in the same diagonal direction.
14+
- Makes **at most one clockwise 90-degree turn** to another diagonal direction while **maintaining** the sequence.
15+
16+
![e](./e.jpg)
17+
18+
Return the **length** of the **longest V-shaped diagonal segment**. If no valid segment exists, return 0.
19+
20+
**Example 1:**
21+
22+
![1](./1.jpg)
23+
24+
```
25+
Input: 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]]
26+
27+
Output: 5
28+
29+
Explanation:
30+
31+
The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,2) → (1,3) → (2,4), takes a 90-degree clockwise turn at (2,4), and continues as (3,3) → (4,2).
32+
```
733

8-
**Example 1:**
34+
**Example 2:**
35+
36+
![2](./2.jpg)
937

1038
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
39+
Input: 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]]
40+
41+
Output: 4
42+
43+
Explanation:
44+
45+
The longest V-shaped diagonal segment has a length of 4 and follows these coordinates: (2,3) → (3,2), takes a 90-degree clockwise turn at (3,2), and continues as (2,1) → (1,0).
46+
```
47+
48+
**Example 3:**
49+
50+
![3](./3.jpg)
51+
1352
```
53+
Input: 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]]
54+
55+
Output: 5
1456
15-
## 题意
16-
> ...
57+
Explanation:
58+
59+
The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: (0,0) → (1,1) → (2,2) → (3,3) → (4,4).
60+
```
1761

18-
## 题解
62+
**Example 4:**
1963

20-
### 思路1
21-
> ...
22-
Length of Longest V-Shaped Diagonal Segment
23-
```go
2464
```
65+
Input: grid = [[1]]
2566
67+
Output: 1
68+
69+
Explanation:
70+
71+
The longest V-shaped diagonal segment has a length of 1 and follows these coordinates: (0,0).
72+
```
2673

2774
## 结语
2875

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(grid [][]int) int {
4+
dirs := [4][2]int{{1, 1}, {1, -1}, {-1, -1}, {-1, 1}}
5+
m, n := len(grid), len(grid[0])
6+
memo := make([][][4][2]int, m)
7+
for i := range memo {
8+
memo[i] = make([][4][2]int, n)
9+
for j := range memo[i] {
10+
for k := range memo[i][j] {
11+
for l := range memo[i][j][k] {
12+
memo[i][j][k][l] = -1
13+
}
14+
}
15+
}
16+
}
17+
18+
var dfs func(cx, cy, direction int, turn bool, target int) int
19+
dfs = func(cx, cy, direction int, turn bool, target int) int {
20+
nx, ny := cx+dirs[direction][0], cy+dirs[direction][1]
21+
/* If it goes beyond the boundary or the next node's value is not the target value, then return */
22+
if nx < 0 || ny < 0 || nx >= m || ny >= n || grid[nx][ny] != target {
23+
return 0
24+
}
25+
26+
turnInt := 0
27+
if turn {
28+
turnInt = 1
29+
}
30+
if memo[nx][ny][direction][turnInt] != -1 {
31+
return memo[nx][ny][direction][turnInt]
32+
}
33+
34+
/* Continue walking in the original direction. */
35+
maxStep := dfs(nx, ny, direction, turn, 2-target)
36+
if turn {
37+
/* Clockwise rotate 90 degrees turn */
38+
maxStep = max(maxStep, dfs(nx, ny, (direction+1)%4, false, 2-target))
39+
}
40+
memo[nx][ny][direction][turnInt] = maxStep + 1
41+
return maxStep + 1
42+
}
43+
44+
res := 0
45+
for i := 0; i < m; i++ {
46+
for j := 0; j < n; j++ {
47+
if grid[i][j] == 1 {
48+
for direction := 0; direction < 4; direction++ {
49+
res = max(res, dfs(i, j, direction, true, 2)+1)
50+
}
51+
}
52+
}
53+
}
54+
return res
555
}

leetcode/3401-3500/3459.Length-of-Longest-V-Shaped-Diagonal-Segment/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{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}}, 5},
17+
{"TestCase2", [][]int{{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}}, 4},
18+
{"TestCase3", [][]int{{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}}, 5},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}
17.7 KB
Loading

0 commit comments

Comments
 (0)