Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 53a9e55

Browse files
committed
Merge pull request #375 from 0xff-dev/931
Add solution and test-cases for problem 931
2 parents 9867724 + 75528ba commit 53a9e55

File tree

5 files changed

+73
-23
lines changed

5 files changed

+73
-23
lines changed

‎leetcode/901-1000/0931.Minimum-Falling-Path-Sum/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [931.Minimum Falling Path Sum][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+
Given an `n x n` array of integers `matrix`, return the **minimum sum** of any **falling path** through `matrix`.
5+
6+
A **falling path** starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position (`row, col`) will be (`row + 1, col - 1`), (`row + 1, col`), or (`row + 1, col + 1`).
7+
8+
**Example 1:**
79

8-
**Example 1:**
10+
![example1](./failing1-grid.jpg)
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: matrix = [[2,1,3],[6,5,4],[7,8,9]]
14+
Output: 13
15+
Explanation: There are two falling paths with a minimum sum as shown.
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
1719

18-
## 题解
20+
![example2](./failing2-grid.jpg)
1921

20-
### 思路1
21-
> ...
22-
Minimum Falling Path Sum
23-
```go
2422
```
25-
23+
Input: matrix = [[-19,57],[-40,-5]]
24+
Output: -59
25+
Explanation: The falling path with a minimum sum is shown.
26+
```
2627

2728
## 结语
2829

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(matrix [][]int) int {
4+
rows, cols := len(matrix), len(matrix[0])
5+
dp := make([][]int, 2)
6+
dp[0], dp[1] = make([]int, cols), make([]int, cols)
7+
visited := make([]bool, cols)
8+
loop := 1
9+
for col := 0; col < cols; col++ {
10+
dp[0][col] = matrix[0][col]
11+
}
12+
13+
for row := 1; row < rows; row++ {
14+
for col := 0; col < cols; col++ {
15+
down := dp[1-loop][col] + matrix[row][col]
16+
if !visited[col] || dp[loop][col] > down {
17+
dp[loop][col] = down
18+
visited[col] = true
19+
}
20+
if col < cols-1 {
21+
downRight := dp[1-loop][col] + matrix[row][col+1]
22+
if !visited[col+1] || dp[loop][col+1] > downRight {
23+
dp[loop][col+1] = downRight
24+
visited[col+1] = true
25+
}
26+
}
27+
if col > 0 {
28+
downLeft := dp[1-loop][col] + matrix[row][col-1]
29+
if !visited[col-1] || dp[loop][col-1] > downLeft {
30+
dp[loop][col-1] = downLeft
31+
visited[col-1] = true
32+
}
33+
}
34+
}
35+
36+
for col := 0; col < cols; col++ {
37+
visited[col] = false
38+
}
39+
loop = 1 - loop
40+
}
41+
ans := dp[1-loop][0]
42+
for i := 1; i < cols; i++ {
43+
if dp[1-loop][i] < ans {
44+
ans = dp[1-loop][i]
45+
}
46+
}
47+
return ans
548
}

‎leetcode/901-1000/0931.Minimum-Falling-Path-Sum/Solution_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ 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{
17+
{2, 1, 3},
18+
{6, 5, 4},
19+
{7, 8, 9},
20+
}, 13},
21+
{"TestCase2", [][]int{
22+
{-19, 57},
23+
{-40, -5},
24+
}, -59},
1925
}
2026

2127
// 开始测试
@@ -30,10 +36,10 @@ func TestSolution(t *testing.T) {
3036
}
3137
}
3238

33-
//压力测试
39+
//压力测试
3440
func BenchmarkSolution(b *testing.B) {
3541
}
3642

37-
//使用案列
43+
//使用案列
3844
func ExampleSolution() {
3945
}
22.7 KB
Loading[フレーム]
9.75 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /