Skip to content

Commit b0b6866

Browse files
committed
一刷1289
1 parent 4f91ca5 commit b0b6866

File tree

6 files changed

+103
-28
lines changed

6 files changed

+103
-28
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9048,14 +9048,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
90489048
//|{doc_base_url}/1288-remove-covered-intervals.adoc[题解]
90499049
//|Medium
90509050
//|
9051-
//
9052-
//|{counter:codes}
9053-
//|{leetcode_base_url}/minimum-falling-path-sum-ii/[1289. Minimum Falling Path Sum II^]
9054-
//|{source_base_url}/_1289_MinimumFallingPathSumII.java[Java]
9055-
//|{doc_base_url}/1289-minimum-falling-path-sum-ii.adoc[题解]
9056-
//|Hard
9057-
//|
9058-
//
9051+
9052+
|{counter:codes}
9053+
|{leetcode_base_url}/minimum-falling-path-sum-ii/[1289. Minimum Falling Path Sum II^]
9054+
|{source_base_url}/_1289_MinimumFallingPathSumII.java[Java]
9055+
|{doc_base_url}/1289-minimum-falling-path-sum-ii.adoc[题解]
9056+
|Hard
9057+
|
9058+
90599059
//|{counter:codes}
90609060
//|{leetcode_base_url}/convert-binary-number-in-a-linked-list-to-integer/[1290. Convert Binary Number in a Linked List to Integer^]
90619061
//|{source_base_url}/_1290_ConvertBinaryNumberInALinkedListToInteger.java[Java]
Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,67 @@
11
[#1289-minimum-falling-path-sum-ii]
2-
= 1289. Minimum Falling Path Sum II
2+
= 1289. 下降路径最小和 II
33

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

6-
Given a square grid of integers `arr`, a _falling path with non-zero shifts_ is a choice of exactly one element from each row of `arr`, such that no two elements chosen in adjacent rows are in the same column.
6+
给你一个 `n x n` 整数矩阵 `grid` ,请你返回 *非零偏移下降路径* 数字和的最小值。
77

8-
Return the minimum sum of a falling path with non-zero shifts.
8+
*非零偏移下降路径* 定义为:从 `grid` 数组中的每一行选择一个数字,且按顺序选出来的数字中,相邻数字不在原数组的同一列。
99

10-
11-
*Example 1:*
10+
*示例 1:*
1211

13-
[subs="verbatim,quotes,macros"]
14-
----
15-
*Input:* arr = [[1,2,3],[4,5,6],[7,8,9]]
16-
*Output:* 13
17-
*Explanation:*
18-
The possible falling paths are:
12+
image::images/1289-01.jpg[{image_attr}]
13+
14+
....
15+
输入:grid = [[1,2,3],[4,5,6],[7,8,9]]
16+
输出:13
17+
解释:
18+
所有非零偏移下降路径包括:
1919
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
2020
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
2121
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
22-
The falling path with the smallest sum is [1,5,7], so the answer is 13.
23-
----
22+
下降路径中数字和最小的是 [1,5,7] ,所以答案是 13 。
23+
....
24+
25+
*示例 2:*
2426

25-
26-
*Constraints:*
27+
....
28+
输入:grid = [[7]]
29+
输出:7
30+
....
2731

32+
*提示:*
2833

29-
* `1 <= arr.length == arr[i].length <= 200`
30-
* `-99 <= arr[i][j] <= 99`
34+
* `n == grid.length == grid[i].length`
35+
* `+1 <= n <= 200+`
36+
* `+-99 <= grid[i][j] <= 99+`
3137
3238
39+
== 思路分析
3340

3441

3542
[[src-1289]]
43+
[tabs]
44+
====
45+
一刷::
46+
+
47+
--
3648
[{java_src_attr}]
3749
----
38-
include::{sourcedir}/_1289_MinimumFallingPathSumII.java[tag=answer]
50+
include::{sourcedir}/_1289_MinimumFallingPathSumIi.java[tag=answer]
3951
----
52+
--
53+
54+
// 二刷::
55+
// +
56+
// --
57+
// [{java_src_attr}]
58+
// ----
59+
// include::{sourcedir}/_1289_MinimumFallingPathSumIi_2.java[tag=answer]
60+
// ----
61+
// --
62+
====
63+
64+
65+
== 参考资料
66+
4067

docs/images/1289-01.jpg

7.49 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2660,7 +2660,7 @@ include::1253-reconstruct-a-2-row-binary-matrix.adoc[leveloffset=+1]
26602660

26612661
// include::1288-remove-covered-intervals.adoc[leveloffset=+1]
26622662

2663-
// include::1289-minimum-falling-path-sum-ii.adoc[leveloffset=+1]
2663+
include::1289-minimum-falling-path-sum-ii.adoc[leveloffset=+1]
26642664

26652665
// include::1290-convert-binary-number-in-a-linked-list-to-integer.adoc[leveloffset=+1]
26662666

logbook/202503.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ endif::[]
330330
|{doc_base_url}/0931-minimum-falling-path-sum.adoc[题解]
331331
|✅ 动态规划
332332

333+
|{counter:codes2503}
334+
|{leetcode_base_url}/minimum-falling-path-sum-ii/[1289. 下降路径最小和 II^]
335+
|{doc_base_url}/1289-minimum-falling-path-sum-ii.adoc[题解]
336+
|⭕️ 动态规划。题目理解错误。
337+
338+
333339
|===
334340

335341
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _1289_MinimumFallingPathSumIi {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-04-18 22:56:34
8+
*/
9+
public int minFallingPathSum(int[][] grid) {
10+
int len = grid.length;
11+
if (len == 1) {
12+
return grid[0][0];
13+
}
14+
int result = Integer.MAX_VALUE;
15+
for (int r = 1; r < len; r++) {
16+
for (int c = 0; c < len; c++) {
17+
int min = Integer.MAX_VALUE;
18+
for (int i = 0; i < len; i++) {
19+
if (i == c) {
20+
continue;
21+
}
22+
min = Math.min(min, grid[r - 1][i]);
23+
}
24+
grid[r][c] += min;
25+
if (r == len - 1) {
26+
result = Math.min(result, grid[r][c]);
27+
}
28+
}
29+
}
30+
return result;
31+
}
32+
// end::answer[]
33+
34+
public static void main(String[] args) {
35+
new _1289_MinimumFallingPathSumIi().minFallingPathSum(new int[][]{
36+
{-73, 61, 43, -48, -36},
37+
{3, 30, 27, 57, 10},
38+
{96, -76, 84, 59, -15},
39+
{5, -49, 76, 31, -7},
40+
{97, 91, 61, -46, 67}});
41+
}
42+
}

0 commit comments

Comments
 (0)