Skip to content

Commit 5efadd0

Browse files
committed
三刷64
1 parent 0ffcaca commit 5efadd0

File tree

4 files changed

+61
-33
lines changed

4 files changed

+61
-33
lines changed

docs/0064-minimum-path-sum.adoc

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,36 @@
11
[#0064-minimum-path-sum]
2-
= 64. Minimum Path Sum
2+
= 64. 最小路径和
33

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

6-
Given a _m x n_ grid filled with non-negative numbers, find a path from top left to bottom right which _minimizes_ the sum of all numbers along its path.
6+
给定一个包含非负整数的 `m x n` 网格 `grid`,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
77

8-
*Note:* You can only move either down or right at any point in time.
8+
**说明:**每次只能向下或者向右移动一步。
99

10-
.Example:
11-
[source]
12-
----
13-
Input:
14-
[
15-
[1,3,1],
16-
[1,5,1],
17-
[4,2,1]
18-
]
19-
Output: 7
20-
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
21-
----
10+
*示例 1:*
2211

23-
image::images/0064-1.png[{image_attr}]
12+
image::images/0064-01.jpg[{image_attr}]
2413

25-
image::images/0064-2.png[{image_attr}]
14+
....
15+
输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
16+
输出:7
17+
解释:因为路径 1→3→1→1→1 的总和最小。
18+
....
2619

27-
思考题:尝试使用一维数组做备忘录来实现一下。
20+
*示例 2:*
2821

29-
Given a _m_ x _n_ grid filled with non-negative numbers, find a path from top left to bottom right which _minimizes_ the sum of all numbers along its path.
22+
....
23+
输入:grid = [[1,2,3],[4,5,6]]
24+
输出:12
25+
....
3026

31-
*Note:* You can only move either down or right at any point in time.
27+
*提示:*
3228

33-
*Example:*
29+
* `+m == grid.length+`
30+
* `+n == grid[i].length+`
31+
* `+1 <= m, n <= 200+`
32+
* `+0 <= grid[i][j] <= 200+`
3433
35-
[subs="verbatim,quotes,macros"]
36-
----
37-
*Input:*
38-
[
39-
[1,3,1],
40-
[1,5,1],
41-
[4,2,1]
42-
]
43-
*Output:* 7
44-
*Explanation:* Because the path 1&rarr;3&rarr;1&rarr;1&rarr;1 minimizes the sum.
45-
----
4634
4735
== 思路分析
4836

@@ -66,6 +54,15 @@ include::{sourcedir}/_0064_MinimumPathSum.java[tag=answer]
6654
include::{sourcedir}/_0064_MinimumPathSum_2.java[tag=answer]
6755
----
6856
--
57+
58+
三刷::
59+
+
60+
--
61+
[{java_src_attr}]
62+
----
63+
include::{sourcedir}/_0064_MinimumPathSum_3.java[tag=answer]
64+
----
65+
--
6966
====
7067

7168
== 参考资料

docs/images/0064-01.jpg

8.3 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ endif::[]
315315
|{doc_base_url}/0980-unique-paths-iii.adoc[题解]
316316
|❌ 明知是回溯,但在处理当前节点时,总把当前和下一步混在一起。有思路,没写出代码。
317317

318+
|{counter:codes2503}
319+
|{leetcode_base_url}/minimum-path-sum/[64. Minimum Path Sum^]
320+
|{doc_base_url}/0064-minimum-path-sum.adoc[题解]
321+
|✅ 动态规划。直接在原有矩阵上操作即可。
322+
318323
|===
319324

320325
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0064_MinimumPathSum_3 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-04-18 21:51:48
8+
*/
9+
public int minPathSum(int[][] grid) {
10+
int row = grid.length;
11+
int col = grid[0].length;
12+
for (int i = 1; i < col; i++) {
13+
grid[0][i] += grid[0][i - 1];
14+
}
15+
for (int i = 1; i < row; i++) {
16+
grid[i][0] += grid[i - 1][0];
17+
}
18+
for (int r = 1; r < row; r++) {
19+
for (int c = 1; c < col; c++) {
20+
grid[r][c] += Math.min(grid[r - 1][c], grid[r][c - 1]);
21+
}
22+
}
23+
return grid[row - 1][col - 1];
24+
}
25+
// end::answer[]
26+
}

0 commit comments

Comments
 (0)