Skip to content

Commit 4f91ca5

Browse files
committed
二刷931
1 parent 0cf85fd commit 4f91ca5

File tree

5 files changed

+71
-31
lines changed

5 files changed

+71
-31
lines changed

docs/0931-minimum-falling-path-sum.adoc

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
[#0931-minimum-falling-path-sum]
2-
= 931. Minimum Falling Path Sum
2+
= 931. 下降路径最小和
33

4-
{leetcode}/problems/minimum-falling-path-sum/[LeetCode - Minimum Falling Path Sum^]
4+
https://leetcode.cn/problems/minimum-falling-path-sum/[LeetCode - 931. 下降路径最小和 ^]
55

6-
Given a *square* array of integers `A`, we want the *minimum* sum of a _falling path_ through `A`.
6+
给你一个 `n x n`*方形* 整数数组 `matrix` ,请你找出并返回通过 `matrix`**下降路径***最小和*
77

8-
A falling path starts at any element in the first row, and chooses one element from each row. The next row's choice must be in a column that is different from the previous row's column by at most one.
8+
*下降路径* 可以从第一行中的任何元素开始,并从每一行中选择一个元素。在下一行选择的元素和当前行所选元素最多相隔一列(即位于正下方或者沿对角线向左或者向右的第一个元素)。具体来说,位置 `(row, col)` 的下一个元素应当是 `(row + 1, col - 1)``(row + 1, col)` 或者 `(row + 1, col + 1)`
99

10-
10+
*示例 1:*
1111

12-
*Example 1:*
13-
14-
[subs="verbatim,quotes,macros"]
15-
----
16-
*Input:* [[1,2,3],[4,5,6],[7,8,9]]
17-
*Output:* 12
18-
*Explanation:*
19-
The possible falling paths are:
20-
----
12+
image::images/0931-01.jpg[{image_attr}]
2113

14+
....
15+
输入:matrix = [[2,1,3],[6,5,4],[7,8,9]]
16+
输出:13
17+
解释:如图所示,为和最小的两条下降路径
18+
....
2219

23-
* `[1,4,7], [1,4,8], [1,5,7], [1,5,8], [1,5,9]`
24-
* `[2,4,7], [2,4,8], [2,5,7], [2,5,8], [2,5,9], [2,6,8], [2,6,9]`
25-
* `[3,5,7], [3,5,8], [3,5,9], [3,6,8], [3,6,9]`
20+
*示例 2:*
2621

22+
image::images/0931-02.jpg[{image_attr}]
2723

28-
The falling path with the smallest sum is `[1,4,7]`, so the answer is `12`.
24+
....
25+
输入:matrix = [[-19,57],[-40,-5]]
26+
输出:-59
27+
解释:如图所示,为和最小的下降路径
28+
....
2929

30-
30+
*提示:*
3131

32-
*Note:*
32+
* `n == matrix.length == matrix[i].length`
33+
* `+1 <= n <= 100+`
34+
* `+-100 <= matrix[i][j] <= 100+`
3335
3436
35-
* `1 <= A.length == A[0].length <= 100`
36-
* `-100 <= A[i][j] <= 100`
37-
3837
== 思路分析
3938

39+
4040
[[src-0931]]
4141
[tabs]
4242
====
@@ -49,14 +49,14 @@ include::{sourcedir}/_0931_MinimumFallingPathSum.java[tag=answer]
4949
----
5050
--
5151
52-
// 二刷::
53-
// +
54-
// --
55-
// [{java_src_attr}]
56-
// ----
57-
// include::{sourcedir}/_0931_MinimumFallingPathSum_2.java[tag=answer]
58-
// ----
59-
// --
52+
二刷::
53+
+
54+
--
55+
[{java_src_attr}]
56+
----
57+
include::{sourcedir}/_0931_MinimumFallingPathSum_2.java[tag=answer]
58+
----
59+
--
6060
====
6161

6262
== 参考资料

docs/images/0931-01.jpg

22.7 KB
Loading

docs/images/0931-02.jpg

9.75 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ endif::[]
325325
|{doc_base_url}/0120-triangle.adoc[题解]
326326
|✅ 动态规划。从底向上比从上向底效率更高。
327327

328+
|{counter:codes2503}
329+
|{leetcode_base_url}/minimum-falling-path-sum/[931. 下降路径最小和^]
330+
|{doc_base_url}/0931-minimum-falling-path-sum.adoc[题解]
331+
|✅ 动态规划
332+
328333
|===
329334

330335
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0931_MinimumFallingPathSum_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-04-18 22:56:51
9+
*/
10+
public int minFallingPathSum(int[][] matrix) {
11+
int len = matrix.length;
12+
if (len == 1) {
13+
return matrix[0][0];
14+
}
15+
int result = Integer.MAX_VALUE;
16+
for (int r = 1; r < len; r++) {
17+
for (int c = 0; c < len; c++) {
18+
int min;
19+
if (c == 0) {
20+
min = Math.min(matrix[r - 1][c], matrix[r - 1][c + 1]);
21+
} else if (c == len - 1) {
22+
min = Math.min(matrix[r - 1][c - 1], matrix[r - 1][c]);
23+
} else {
24+
min = Math.min(matrix[r - 1][c], Math.min(matrix[r - 1][c - 1], matrix[r - 1][c + 1]));
25+
}
26+
matrix[r][c] = matrix[r][c] + min;
27+
if (r == len - 1) {
28+
result = Math.min(result, matrix[r][c]);
29+
}
30+
}
31+
}
32+
return result;
33+
}
34+
// end::answer[]
35+
}

0 commit comments

Comments
 (0)