Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 0fdb6c9

Browse files
aQuaaQua
aQua
authored and
aQua
committed
329 added
1 parent 25a2e60 commit 0fdb6c9

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# [329. Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/)
2+
3+
## 题目
4+
5+
Given an integer matrix, find the length of the longest increasing path.
6+
7+
From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed).
8+
9+
Example 1:
10+
11+
```text
12+
nums = [
13+
[9,9,4],
14+
[6,6,8],
15+
[2,1,1]
16+
]
17+
Return 4
18+
The longest increasing path is [1, 2, 6, 9].
19+
```
20+
21+
Example 2:
22+
23+
```text
24+
nums = [
25+
[3,4,5],
26+
[3,2,6],
27+
[2,2,1]
28+
]
29+
Return 4
30+
The longest increasing path is [3, 4, 5, 6]. Moving diagonally is not allowed.
31+
```
32+
33+
Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases.
34+
35+
## 解题思路
36+
37+
见程序注释
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package Problem0329
2+
3+
func longestIncreasingPath(matrix [][]int) int {
4+
5+
return 4
6+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package Problem0329
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
matrix [][]int
13+
ans int
14+
}{
15+
16+
{[][]int{
17+
[]int{9, 9, 4},
18+
[]int{6, 6, 8},
19+
[]int{2, 1, 1},
20+
},
21+
4},
22+
23+
{[][]int{
24+
[]int{3, 4, 5},
25+
[]int{3, 2, 6},
26+
[]int{2, 2, 1},
27+
},
28+
4},
29+
30+
// 可以有多个 testcase
31+
}
32+
33+
func Test_longestIncreasingPath(t *testing.T) {
34+
ast := assert.New(t)
35+
36+
for _, tc := range tcs {
37+
fmt.Printf("~~%v~~\n", tc)
38+
ast.Equal(tc.ans, longestIncreasingPath(tc.matrix), "输入:%v", tc)
39+
}
40+
}
41+
42+
func Benchmark_longestIncreasingPath(b *testing.B) {
43+
for i := 0; i < b.N; i++ {
44+
for _, tc := range tcs {
45+
longestIncreasingPath(tc.matrix)
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)