Skip to content

Commit aafa116

Browse files
committed
feat: add solutions to lc problem: No.0045. Jump Game II
1 parent ea296d6 commit aafa116

File tree

9 files changed

+239
-34
lines changed

9 files changed

+239
-34
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
- [打家劫舍 II](./solution/0200-0299/0213.House%20Robber%20II/README.md)
167167
- [删除并获得点数](./solution/0700-0799/0740.Delete%20and%20Earn/README.md)
168168
- [跳跃游戏](./solution/0000-0099/0055.Jump%20Game/README.md)
169+
- [跳跃游戏 II](./solution/0000-0099/0045.Jump%20Game%20II/README.md)
169170
- [接雨水](./solution/0000-0099/0042.Trapping%20Rain%20Water/README.md)
170171
- [最大子序和](./solution/0000-0099/0053.Maximum%20Subarray/README.md)
171172
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
@@ -247,6 +248,4 @@
247248

248249
## 许可证
249250

250-
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a>
251-
252-
<a href="https://app.fossa.com/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode?ref=badge_large" target="_blank"><img src="https://app.fossa.com/api/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode.svg?type=large"></a>
251+
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a>

README_EN.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
160160
- [House Robber II](./solution/0200-0299/0213.House%20Robber%20II/README_EN.md)
161161
- [Delete and Earn](./solution/0700-0799/0740.Delete%20and%20Earn/README_EN.md)
162162
- [Jump Game](./solution/0000-0099/0055.Jump%20Game/README_EN.md)
163+
- [Jump Game II](./solution/0000-0099/0045.Jump%20Game%20II/README_EN.md)
163164
- [Trapping Rain Water](./solution/0000-0099/0042.Trapping%20Rain%20Water/README_EN.md)
164165
- [Maximum Subarray](./solution/0000-0099/0053.Maximum%20Subarray/README_EN.md)
165166
- [Minimum Path Sum](./solution/0000-0099/0064.Minimum%20Path%20Sum/README_EN.md)
@@ -231,6 +232,4 @@ Thank you to all our backers and sponsors!
231232
232233
## License
233234

234-
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>.
235-
236-
<a href="https://app.fossa.com/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode?ref=badge_large" target="_blank"><img src="https://app.fossa.com/api/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode.svg?type=large"></a>
235+
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>.

solution/0000-0099/0045.Jump Game II/README.md

+89-1
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,110 @@
2929

3030
<!-- 这里可写通用的实现逻辑 -->
3131

32+
贪心。
33+
3234
<!-- tabs:start -->
3335

3436
### **Python3**
3537

3638
<!-- 这里可写当前语言的特殊实现逻辑 -->
3739

3840
```python
39-
41+
class Solution:
42+
def jump(self, nums: List[int]) -> int:
43+
end = mx = steps = 0
44+
for i, num in enumerate(nums[:-1]):
45+
mx = max(mx, i + num)
46+
if i == end:
47+
end = mx
48+
steps += 1
49+
return steps
4050
```
4151

4252
### **Java**
4353

4454
<!-- 这里可写当前语言的特殊实现逻辑 -->
4555

4656
```java
57+
class Solution {
58+
public int jump(int[] nums) {
59+
int end = 0;
60+
int mx = 0;
61+
int steps = 0;
62+
for (int i = 0; i < nums.length - 1; ++i) {
63+
mx = Math.max(mx, i + nums[i]);
64+
if (i == end) {
65+
end = mx;
66+
++steps;
67+
}
68+
}
69+
return steps;
70+
}
71+
}
72+
```
73+
74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
int jump(vector<int>& nums) {
80+
int mx = 0, steps = 0, end = 0;
81+
for (int i = 0; i < nums.size() - 1; ++i) {
82+
mx = max(mx, i + nums[i]);
83+
if (i == end) {
84+
end = mx;
85+
++steps;
86+
}
87+
}
88+
return steps;
89+
}
90+
};
91+
```
92+
93+
### **Go**
94+
95+
```go
96+
func jump(nums []int) int {
97+
mx, steps, end := 0, 0, 0
98+
for i := 0; i < len(nums)-1; i++ {
99+
mx = max(mx, i+nums[i])
100+
if i == end {
101+
end = mx
102+
steps++
103+
}
104+
}
105+
return steps
106+
}
107+
108+
func max(a, b int) int {
109+
if a > b {
110+
return a
111+
}
112+
return b
113+
}
114+
```
47115

116+
### **C#**
117+
118+
```cs
119+
public class Solution {
120+
public int Jump(int[] nums) {
121+
int end = 0;
122+
int mx = 0;
123+
int steps = 0;
124+
for (int i = 0; i < nums.Length - 1; ++i)
125+
{
126+
mx = Math.Max(mx, i + nums[i]);
127+
if (i == end)
128+
{
129+
end = mx;
130+
++steps;
131+
}
132+
}
133+
return steps;
134+
}
135+
}
48136
```
49137

50138
### **...**

solution/0000-0099/0045.Jump Game II/README_EN.md

+87-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,99 @@
4444
### **Python3**
4545

4646
```python
47-
47+
class Solution:
48+
def jump(self, nums: List[int]) -> int:
49+
end = mx = steps = 0
50+
for i, num in enumerate(nums[:-1]):
51+
mx = max(mx, i + num)
52+
if i == end:
53+
end = mx
54+
steps += 1
55+
return steps
4856
```
4957

5058
### **Java**
5159

5260
```java
61+
class Solution {
62+
public int jump(int[] nums) {
63+
int end = 0;
64+
int mx = 0;
65+
int steps = 0;
66+
for (int i = 0; i < nums.length - 1; ++i) {
67+
mx = Math.max(mx, i + nums[i]);
68+
if (i == end) {
69+
end = mx;
70+
++steps;
71+
}
72+
}
73+
return steps;
74+
}
75+
}
76+
```
77+
78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
int jump(vector<int>& nums) {
84+
int mx = 0, steps = 0, end = 0;
85+
for (int i = 0; i < nums.size() - 1; ++i) {
86+
mx = max(mx, i + nums[i]);
87+
if (i == end) {
88+
end = mx;
89+
++steps;
90+
}
91+
}
92+
return steps;
93+
}
94+
};
95+
```
96+
97+
### **Go**
98+
99+
```go
100+
func jump(nums []int) int {
101+
mx, steps, end := 0, 0, 0
102+
for i := 0; i < len(nums)-1; i++ {
103+
mx = max(mx, i+nums[i])
104+
if i == end {
105+
end = mx
106+
steps++
107+
}
108+
}
109+
return steps
110+
}
111+
112+
func max(a, b int) int {
113+
if a > b {
114+
return a
115+
}
116+
return b
117+
}
118+
```
53119

120+
### **C#**
121+
122+
```cs
123+
public class Solution {
124+
public int Jump(int[] nums) {
125+
int end = 0;
126+
int mx = 0;
127+
int steps = 0;
128+
for (int i = 0; i < nums.Length - 1; ++i)
129+
{
130+
mx = Math.Max(mx, i + nums[i]);
131+
if (i == end)
132+
{
133+
end = mx;
134+
++steps;
135+
}
136+
}
137+
return steps;
138+
}
139+
}
54140
```
55141

56142
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int jump(vector<int>& nums) {
4+
int mx = 0, steps = 0, end = 0;
5+
for (int i = 0; i < nums.size() - 1; ++i) {
6+
mx = max(mx, i + nums[i]);
7+
if (i == end) {
8+
end = mx;
9+
++steps;
10+
}
11+
}
12+
return steps;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
1-
using System;
2-
31
public class Solution {
42
public int Jump(int[] nums) {
5-
var steps = 0;
6-
var maxJump = 0;
7-
var i = 0;
8-
while (maxJump + 1 < nums.Length)
3+
int end = 0;
4+
int mx = 0;
5+
int steps = 0;
6+
for (int i = 0; i < nums.Length - 1; ++i)
97
{
10-
var newMaxJump = maxJump;
11-
for (var j = i; j < nums.Length && j <= maxJump; ++j)
8+
mx = Math.Max(mx, i + nums[i]);
9+
if (i == end)
1210
{
13-
newMaxJump = Math.Max(newMaxJump, j + nums[j]);
14-
}
15-
i = maxJump + 1;
16-
if (newMaxJump > maxJump)
17-
{
18-
maxJump = newMaxJump;
11+
end = mx;
1912
++steps;
2013
}
21-
else
22-
{
23-
break;
24-
}
2514
}
26-
if (maxJump + 1 >= nums.Length) return steps;
27-
return -1;
15+
return steps;
2816
}
2917
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func jump(nums []int) int {
2+
mx, steps, end := 0, 0, 0
3+
for i := 0; i < len(nums)-1; i++ {
4+
mx = max(mx, i+nums[i])
5+
if i == end {
6+
end = mx
7+
steps++
8+
}
9+
}
10+
return steps
11+
}
12+
13+
func max(a, b int) int {
14+
if a > b {
15+
return a
16+
}
17+
return b
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
class Solution {
22
public int jump(int[] nums) {
3-
int cnt = 0,last = 0, next = 1;
4-
for (;next < nums.length;cnt++){
5-
int i = last;
6-
last = next;
7-
for (; i < last; ++i) if (i + nums[i] >= next) next = i + nums[i] + 1;
3+
int end = 0;
4+
int mx = 0;
5+
int steps = 0;
6+
for (int i = 0; i < nums.length - 1; ++i) {
7+
mx = Math.max(mx, i + nums[i]);
8+
if (i == end) {
9+
end = mx;
10+
++steps;
11+
}
812
}
9-
return cnt;
13+
return steps;
1014
}
1115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def jump(self, nums: List[int]) -> int:
3+
end = mx = steps = 0
4+
for i, num in enumerate(nums[:-1]):
5+
mx = max(mx, i + num)
6+
if i == end:
7+
end = mx
8+
steps += 1
9+
return steps

0 commit comments

Comments
 (0)