Skip to content

Commit 1a813d9

Browse files
committedMay 9, 2022
feat: add solutions to lcp problem: No.09
No.09.最小跳跃次数
1 parent 14dbe7a commit 1a813d9

File tree

5 files changed

+253
-1
lines changed

5 files changed

+253
-1
lines changed
 

‎lcp/LCP 09. 最小跳跃次数/README.md

+132-1
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,153 @@
2929

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

32+
**方法一:BFS**
33+
3234
<!-- tabs:start -->
3335

3436
### **Python3**
3537

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

3840
```python
39-
41+
class Solution:
42+
def minJump(self, jump: List[int]) -> int:
43+
n = len(jump)
44+
vis = [False] * (n + 1)
45+
q = deque([0])
46+
ans = 0
47+
vis[0] = True
48+
mx = 1
49+
while q:
50+
for _ in range(len(q)):
51+
i = q.popleft()
52+
if i + jump[i] >= n:
53+
return ans + 1
54+
for j in list(range(mx, i)) + [i + jump[i]]:
55+
if not vis[j]:
56+
q.append(j)
57+
vis[j] = True
58+
mx = max(mx, i + 1)
59+
ans += 1
60+
return -1
4061
```
4162

4263
### **Java**
4364

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

4667
```java
68+
class Solution {
69+
public int minJump(int[] jump) {
70+
int n = jump.length;
71+
boolean[] vis = new boolean[n + 1];
72+
Deque<Integer> q = new ArrayDeque<>();
73+
q.offer(0);
74+
vis[0] = true;
75+
int ans = 0;
76+
int mx = 1;
77+
while (!q.isEmpty()) {
78+
for (int t = q.size(); t > 0; --t) {
79+
int i = q.poll();
80+
int j = i + jump[i];
81+
if (j >= n) {
82+
return ans + 1;
83+
}
84+
if (!vis[j]) {
85+
q.offer(j);
86+
vis[j] = true;
87+
}
88+
for (j = mx; j < i; ++j) {
89+
if (!vis[j]) {
90+
q.offer(j);
91+
vis[j] = true;
92+
}
93+
}
94+
mx = Math.max(mx, i + 1);
95+
}
96+
++ans;
97+
}
98+
return -1;
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
int minJump(vector<int>& jump) {
109+
int n = jump.size();
110+
vector<bool> vis(n + 1);
111+
queue<int> q{{0}};
112+
vis[0] = true;
113+
int ans = 0, mx = 1;
114+
while (!q.empty())
115+
{
116+
for (int t = q.size(); t; --t)
117+
{
118+
int i = q.front();
119+
int j = i + jump[i];
120+
if (j >= n) return ans + 1;
121+
q.pop();
122+
if (!vis[j])
123+
{
124+
vis[j] = true;
125+
q.push(j);
126+
}
127+
for (j = mx; j < i; ++j)
128+
{
129+
if (!vis[j])
130+
{
131+
vis[j] = true;
132+
q.push(j);
133+
}
134+
}
135+
mx = max(mx, i + 1);
136+
}
137+
++ans;
138+
}
139+
return -1;
140+
}
141+
};
142+
```
47143
144+
### **Go**
145+
146+
```go
147+
func minJump(jump []int) int {
148+
n := len(jump)
149+
vis := make([]bool, n + 1)
150+
q := []int{0}
151+
vis[0] = true
152+
ans, mx := 0, 1
153+
for len(q) > 0 {
154+
for t := len(q); t > 0; t-- {
155+
i := q[0]
156+
q = q[1:]
157+
j := i + jump[i]
158+
if j >= n {
159+
return ans + 1
160+
}
161+
if !vis[j] {
162+
vis[j] = true
163+
q = append(q, j)
164+
}
165+
for j = mx; j < i; j++ {
166+
if !vis[j] {
167+
vis[j] = true
168+
q = append(q, j)
169+
}
170+
}
171+
if mx < i + 1 {
172+
mx = i + 1
173+
}
174+
}
175+
ans++
176+
}
177+
return -1
178+
}
48179
```
49180

50181
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
int minJump(vector<int>& jump) {
4+
int n = jump.size();
5+
vector<bool> vis(n + 1);
6+
queue<int> q{{0}};
7+
vis[0] = true;
8+
int ans = 0, mx = 1;
9+
while (!q.empty())
10+
{
11+
for (int t = q.size(); t; --t)
12+
{
13+
int i = q.front();
14+
int j = i + jump[i];
15+
if (j >= n) return ans + 1;
16+
q.pop();
17+
if (!vis[j])
18+
{
19+
vis[j] = true;
20+
q.push(j);
21+
}
22+
for (j = mx; j < i; ++j)
23+
{
24+
if (!vis[j])
25+
{
26+
vis[j] = true;
27+
q.push(j);
28+
}
29+
}
30+
mx = max(mx, i + 1);
31+
}
32+
++ans;
33+
}
34+
return -1;
35+
}
36+
};
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func minJump(jump []int) int {
2+
n := len(jump)
3+
vis := make([]bool, n+1)
4+
q := []int{0}
5+
vis[0] = true
6+
ans, mx := 0, 1
7+
for len(q) > 0 {
8+
for t := len(q); t > 0; t-- {
9+
i := q[0]
10+
q = q[1:]
11+
j := i + jump[i]
12+
if j >= n {
13+
return ans + 1
14+
}
15+
if !vis[j] {
16+
vis[j] = true
17+
q = append(q, j)
18+
}
19+
for j = mx; j < i; j++ {
20+
if !vis[j] {
21+
vis[j] = true
22+
q = append(q, j)
23+
}
24+
}
25+
if mx < i+1 {
26+
mx = i + 1
27+
}
28+
}
29+
ans++
30+
}
31+
return -1
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public int minJump(int[] jump) {
3+
int n = jump.length;
4+
boolean[] vis = new boolean[n + 1];
5+
Deque<Integer> q = new ArrayDeque<>();
6+
q.offer(0);
7+
vis[0] = true;
8+
int ans = 0;
9+
int mx = 1;
10+
while (!q.isEmpty()) {
11+
for (int t = q.size(); t > 0; --t) {
12+
int i = q.poll();
13+
int j = i + jump[i];
14+
if (j >= n) {
15+
return ans + 1;
16+
}
17+
if (!vis[j]) {
18+
q.offer(j);
19+
vis[j] = true;
20+
}
21+
for (j = mx; j < i; ++j) {
22+
if (!vis[j]) {
23+
q.offer(j);
24+
vis[j] = true;
25+
}
26+
}
27+
mx = Math.max(mx, i + 1);
28+
}
29+
++ans;
30+
}
31+
return -1;
32+
}
33+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def minJump(self, jump: List[int]) -> int:
3+
n = len(jump)
4+
vis = [False] * (n + 1)
5+
q = deque([0])
6+
ans = 0
7+
vis[0] = True
8+
mx = 1
9+
while q:
10+
for _ in range(len(q)):
11+
i = q.popleft()
12+
if i + jump[i] >= n:
13+
return ans + 1
14+
for j in list(range(mx, i)) + [i + jump[i]]:
15+
if not vis[j]:
16+
q.append(j)
17+
vis[j] = True
18+
mx = max(mx, i + 1)
19+
ans += 1
20+
return -1

0 commit comments

Comments
 (0)