Skip to content

Commit aaa00d4

Browse files
committed
二刷45
1 parent f178674 commit aaa00d4

File tree

6 files changed

+129
-15
lines changed

6 files changed

+129
-15
lines changed

docs/0045-jump-game-ii.adoc

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,77 @@
11
[#0045-jump-game-ii]
2-
= 45. Jump Game II
2+
= 45. 跳跃游戏 II
33

4-
{leetcode}/problems/jump-game-ii/[LeetCode - Jump Game II^]
4+
https://leetcode.cn/problems/jump-game-ii/[LeetCode - 45. 跳跃游戏 II ^]
55

6-
Given an array of non-negative integers, you are initially positioned at the first index of the array.
6+
给定一个长度为 `n`**0 索引**整数数组 `nums`。初始位置为 `+nums[0]+`
77

8-
Each element in the array represents your maximum jump length at that position.
8+
每个元素 `nums[i]` 表示从索引 `i` 向前跳转的最大长度。换句话说,如果你在 `nums[i]` 处,你可以跳转到任意 `nums[i + j]` 处:
99

10-
Your goal is to reach the last index in the minimum number of jumps.
10+
* `+0 <= j <= nums[i]+`
11+
* `i + j < n`
1112
12-
*Example:*
13+
返回到达 `nums[n - 1]` 的最小跳跃次数。生成的测试用例可以到达 `nums[n - 1]`
1314

14-
[subs="verbatim,quotes,macros"]
15-
----
16-
*Input:* [2,3,1,1,4]
17-
*Output:* 2
18-
*Explanation:* The minimum number of jumps to reach the last index is 2.
19-
Jump 1 step from index 0 to 1, then 3 steps to the last index.
20-
----
15+
*示例 1:*
16+
17+
....
18+
输入: nums = [2,3,1,1,4]
19+
输出: 2
20+
解释: 跳到最后一个位置的最小跳跃数是 2。
21+
从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
22+
....
23+
24+
*示例 2:*
25+
26+
....
27+
输入: nums = [2,3,0,1,4]
28+
输出: 2
29+
....
2130

22-
*Note:*
31+
*提示:*
2332

24-
You can assume that you can always reach the last index.
33+
* `1 \<= nums.length \<= 10^4^`
34+
* `+0 <= nums[i] <= 1000+`
35+
* 题目保证可以到达 `nums[n-1]`
36+
37+
38+
== 思路分析
39+
40+
image::images/0045-10.png[{image_attr}]
2541

2642

2743
[[src-0045]]
44+
[tabs]
45+
====
46+
一刷::
47+
+
48+
--
2849
[{java_src_attr}]
2950
----
3051
include::{sourcedir}/_0045_JumpGameII.java[tag=answer]
3152
----
53+
--
54+
55+
二刷::
56+
+
57+
--
58+
[{java_src_attr}]
59+
----
60+
include::{sourcedir}/_0045_JumpGameIi_2.java[tag=answer]
61+
----
62+
--
63+
64+
二刷(优化)::
65+
+
66+
--
67+
[{java_src_attr}]
68+
----
69+
include::{sourcedir}/_0045_JumpGameIi_20.java[tag=answer]
70+
----
71+
--
72+
====
73+
74+
75+
== 参考资料
76+
3277

docs/images/0045-10.png

115 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,11 @@ endif::[]
718718
|{doc_base_url}/0026-remove-duplicates-from-sorted-array.adoc[题解]
719719
|✅ 双指针
720720

721+
|{counter:codes2503}
722+
|{leetcode_base_url}/jump-game-ii/[45. 跳跃游戏 II^]
723+
|{doc_base_url}/0045-jump-game-ii.adoc[题解]
724+
|✅ 贪心算法。也可以动态规划。
725+
721726
|===
722727

723728
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0045_JumpGameII.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class _0045_JumpGameII {
3535
* Runtime: 1 ms, faster than 99.98% of Java online submissions for Jump Game II.
3636
*
3737
* Memory Usage: 36.4 MB, less than 100.00% of Java online submissions for Jump Game II.
38+
*
39+
* @author D瓜哥 · https://www.diguage.com
40+
* @since 2019-10-26 11:29
3841
*/
3942
public int jump(int[] nums) {
4043
if (Objects.isNull(nums) || nums.length == 0) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class _0045_JumpGameIi_2 {
6+
// tag::answer[]
7+
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-05-17 21:14:27
11+
*/
12+
public int jump(int[] nums) {
13+
int length = nums.length;
14+
int[] dp = new int[length];
15+
Arrays.fill(dp, length + 1);
16+
dp[0] = 0;
17+
for (int i = 0; i < length; i++) {
18+
int min = Math.min(length - 1, i + nums[i]);
19+
for (int j = i + 1; j <= min; j++) {
20+
dp[j] = Math.min(dp[j], dp[i] + 1);
21+
}
22+
}
23+
return dp[length - 1];
24+
}
25+
// end::answer[]
26+
public static void main(String[] args) {
27+
new _0045_JumpGameIi_2()
28+
.jump(new int[]{2, 3, 1, 1, 4});
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0045_JumpGameIi_20 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-05-17 21:14:27
8+
*/
9+
public int jump(int[] nums) {
10+
int length = nums.length;
11+
int max = 0;
12+
int step = 0;
13+
int end = 0;
14+
for (int i = 0; i < length; i++) {
15+
max = Math.max(max, i + nums[i]);
16+
if (max >= length - 1) {
17+
return step + 1;
18+
}
19+
if (i == end) {
20+
end = max;
21+
step++;
22+
}
23+
}
24+
return step;
25+
}
26+
// end::answer[]
27+
public static void main(String[] args) {
28+
new _0045_JumpGameIi_20()
29+
.jump(new int[]{2, 3, 1, 1, 4});
30+
}
31+
}

0 commit comments

Comments
 (0)