Skip to content

Commit 14dbe7a

Browse files
committed
feat: add solutions to lc problem: No.1654
No.1654.Minimum Jumps to Reach Home
1 parent 4be97ba commit 14dbe7a

File tree

8 files changed

+438
-4
lines changed

8 files changed

+438
-4
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173

174174
感谢以下所有朋友对本项目的贡献!
175175

176-
<a href="https://github.com/doocs/leetcode/graphs/contributors" target="_blank"><img src="https://contrib.rocks/image?repo=doocs/leetcode" /></a>
176+
<a href="https://github.com/doocs/leetcode/graphs/contributors" target="_blank"><img src="https://contrib.rocks/image?repo=doocs/leetcode&max=500" /></a>
177177

178178
## 赞助者
179179

README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ You can also contribute to [doocs/leetcode](https://github.com/doocs/leetcode) u
165165

166166
This project exists thanks to all the people who contribute.
167167

168-
<a href="https://github.com/doocs/leetcode/graphs/contributors" target="_blank"><img src="https://contrib.rocks/image?repo=doocs/leetcode" /></a>
168+
<a href="https://github.com/doocs/leetcode/graphs/contributors" target="_blank"><img src="https://contrib.rocks/image?repo=doocs/leetcode&max=500" /></a>
169169

170170
## Backers & Sponsors
171171

solution/1600-1699/1654.Minimum Jumps to Reach Home/README.md

+149-1
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,170 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:BFS**
66+
6567
<!-- tabs:start -->
6668

6769
### **Python3**
6870

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

7173
```python
72-
74+
class Solution:
75+
def minimumJumps(self, forbidden: List[int], a: int, b: int, x: int) -> int:
76+
s = set(forbidden)
77+
q = deque([(0, 0)])
78+
vis = {(0, 1), (0, -1)}
79+
ans = 0
80+
while q:
81+
for _ in range(len(q)):
82+
i, dir = q.popleft()
83+
if i == x:
84+
return ans
85+
nxt = [(i + a, 1)]
86+
if dir != -1:
87+
nxt.append((i - b, -1))
88+
for j, dir in nxt:
89+
if 0 <= j <= 6000 and j not in s and (j, dir) not in vis:
90+
vis.add((j, dir))
91+
q.append((j, dir))
92+
ans += 1
93+
return -1
7394
```
7495

7596
### **Java**
7697

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

79100
```java
101+
class Solution {
102+
private static final int N = 6010;
103+
104+
public int minimumJumps(int[] forbidden, int a, int b, int x) {
105+
Set<Integer> s = new HashSet<>();
106+
for (int v : forbidden) {
107+
s.add(v);
108+
}
109+
Deque<int[]> q = new ArrayDeque<>();
110+
q.offer(new int[]{0, 2});
111+
boolean[][] vis = new boolean[N][2];
112+
vis[0][0] = true;
113+
vis[0][1] = true;
114+
int ans = 0;
115+
while (!q.isEmpty()) {
116+
for (int t = q.size(); t > 0; --t) {
117+
int[] p = q.poll();
118+
int i = p[0], dir = p[1];
119+
if (i == x) {
120+
return ans;
121+
}
122+
List<int[]> nxt = new ArrayList<>();
123+
nxt.add(new int[]{i + a, 1});
124+
if (dir != 0) {
125+
nxt.add(new int[]{i - b, 0});
126+
}
127+
for (int[] e : nxt) {
128+
int j = e[0];
129+
dir = e[1];
130+
if (j >= 0 && j < N && !s.contains(j) && !vis[j][dir]) {
131+
vis[j][dir] = true;
132+
q.offer(new int[]{j, dir});
133+
}
134+
}
135+
}
136+
++ans;
137+
}
138+
return -1;
139+
}
140+
}
141+
```
142+
143+
### **C++**
144+
145+
```cpp
146+
class Solution {
147+
public:
148+
const int N = 6010;
149+
150+
int minimumJumps(vector<int>& forbidden, int a, int b, int x) {
151+
unordered_set<int> s(forbidden.begin(), forbidden.end());
152+
queue<vector<int>> q;
153+
q.push({0, 0});
154+
vector<vector<bool>> vis(N, vector<bool>(2));
155+
vis[0][0] = true;
156+
vis[0][1] = true;
157+
int ans = 0;
158+
while (!q.empty())
159+
{
160+
for (int t = q.size(); t; --t)
161+
{
162+
auto p = q.front();
163+
q.pop();
164+
int i = p[0], dir = p[1];
165+
if (i == x) return ans;
166+
vector<vector<int>> nxt;
167+
nxt.push_back({i + a, 1});
168+
if (dir) nxt.push_back({i - b, 0});
169+
for (auto& e : nxt)
170+
{
171+
int j = e[0];
172+
dir = e[1];
173+
if (j >= 0 && j < N && !s.count(j) && !vis[j][dir])
174+
{
175+
vis[j][dir] = true;
176+
q.push({j, dir});
177+
}
178+
}
179+
}
180+
++ans;
181+
}
182+
return -1;
183+
}
184+
};
185+
```
80186

187+
### **Go**
188+
189+
```go
190+
func minimumJumps(forbidden []int, a int, b int, x int) int {
191+
n := 6010
192+
s := make(map[int]bool)
193+
for _, v := range forbidden {
194+
s[v] = true
195+
}
196+
q := [][]int{[]int{0, 0}}
197+
vis := make([][]bool, n)
198+
for i := range vis {
199+
vis[i] = make([]bool, 2)
200+
}
201+
vis[0][0] = true
202+
vis[0][1] = true
203+
ans := 0
204+
for len(q) > 0 {
205+
for t := len(q); t > 0; t-- {
206+
p := q[0]
207+
q = q[1:]
208+
i, dir := p[0], p[1]
209+
if i == x {
210+
return ans
211+
}
212+
nxt := [][]int{[]int{i + a, 1}}
213+
if dir != 0 {
214+
nxt = append(nxt, []int{i - b, 0})
215+
}
216+
for _, e := range nxt {
217+
j := e[0]
218+
dir = e[1]
219+
if j >= 0 && j < n && !s[j] && !vis[j][dir] {
220+
vis[j][dir] = true
221+
q = append(q, []int{j, dir})
222+
}
223+
}
224+
}
225+
ans++
226+
}
227+
return -1
228+
}
81229
```
82230

83231
### **...**

solution/1600-1699/1654.Minimum Jumps to Reach Home/README_EN.md

+149-1
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,166 @@
5656

5757
## Solutions
5858

59+
BFS.
60+
5961
<!-- tabs:start -->
6062

6163
### **Python3**
6264

6365
```python
64-
66+
class Solution:
67+
def minimumJumps(self, forbidden: List[int], a: int, b: int, x: int) -> int:
68+
s = set(forbidden)
69+
q = deque([(0, 0)])
70+
vis = {(0, 1), (0, -1)}
71+
ans = 0
72+
while q:
73+
for _ in range(len(q)):
74+
i, dir = q.popleft()
75+
if i == x:
76+
return ans
77+
nxt = [(i + a, 1)]
78+
if dir != -1:
79+
nxt.append((i - b, -1))
80+
for j, dir in nxt:
81+
if 0 <= j <= 6000 and j not in s and (j, dir) not in vis:
82+
vis.add((j, dir))
83+
q.append((j, dir))
84+
ans += 1
85+
return -1
6586
```
6687

6788
### **Java**
6889

6990
```java
91+
class Solution {
92+
private static final int N = 6010;
93+
94+
public int minimumJumps(int[] forbidden, int a, int b, int x) {
95+
Set<Integer> s = new HashSet<>();
96+
for (int v : forbidden) {
97+
s.add(v);
98+
}
99+
Deque<int[]> q = new ArrayDeque<>();
100+
q.offer(new int[]{0, 2});
101+
boolean[][] vis = new boolean[N][2];
102+
vis[0][0] = true;
103+
vis[0][1] = true;
104+
int ans = 0;
105+
while (!q.isEmpty()) {
106+
for (int t = q.size(); t > 0; --t) {
107+
int[] p = q.poll();
108+
int i = p[0], dir = p[1];
109+
if (i == x) {
110+
return ans;
111+
}
112+
List<int[]> nxt = new ArrayList<>();
113+
nxt.add(new int[]{i + a, 1});
114+
if (dir != 0) {
115+
nxt.add(new int[]{i - b, 0});
116+
}
117+
for (int[] e : nxt) {
118+
int j = e[0];
119+
dir = e[1];
120+
if (j >= 0 && j < N && !s.contains(j) && !vis[j][dir]) {
121+
vis[j][dir] = true;
122+
q.offer(new int[]{j, dir});
123+
}
124+
}
125+
}
126+
++ans;
127+
}
128+
return -1;
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
const int N = 6010;
139+
140+
int minimumJumps(vector<int>& forbidden, int a, int b, int x) {
141+
unordered_set<int> s(forbidden.begin(), forbidden.end());
142+
queue<vector<int>> q;
143+
q.push({0, 0});
144+
vector<vector<bool>> vis(N, vector<bool>(2));
145+
vis[0][0] = true;
146+
vis[0][1] = true;
147+
int ans = 0;
148+
while (!q.empty())
149+
{
150+
for (int t = q.size(); t; --t)
151+
{
152+
auto p = q.front();
153+
q.pop();
154+
int i = p[0], dir = p[1];
155+
if (i == x) return ans;
156+
vector<vector<int>> nxt;
157+
nxt.push_back({i + a, 1});
158+
if (dir) nxt.push_back({i - b, 0});
159+
for (auto& e : nxt)
160+
{
161+
int j = e[0];
162+
dir = e[1];
163+
if (j >= 0 && j < N && !s.count(j) && !vis[j][dir])
164+
{
165+
vis[j][dir] = true;
166+
q.push({j, dir});
167+
}
168+
}
169+
}
170+
++ans;
171+
}
172+
return -1;
173+
}
174+
};
175+
```
70176

177+
### **Go**
178+
179+
```go
180+
func minimumJumps(forbidden []int, a int, b int, x int) int {
181+
n := 6010
182+
s := make(map[int]bool)
183+
for _, v := range forbidden {
184+
s[v] = true
185+
}
186+
q := [][]int{[]int{0, 0}}
187+
vis := make([][]bool, n)
188+
for i := range vis {
189+
vis[i] = make([]bool, 2)
190+
}
191+
vis[0][0] = true
192+
vis[0][1] = true
193+
ans := 0
194+
for len(q) > 0 {
195+
for t := len(q); t > 0; t-- {
196+
p := q[0]
197+
q = q[1:]
198+
i, dir := p[0], p[1]
199+
if i == x {
200+
return ans
201+
}
202+
nxt := [][]int{[]int{i + a, 1}}
203+
if dir != 0 {
204+
nxt = append(nxt, []int{i - b, 0})
205+
}
206+
for _, e := range nxt {
207+
j := e[0]
208+
dir = e[1]
209+
if j >= 0 && j < n && !s[j] && !vis[j][dir] {
210+
vis[j][dir] = true
211+
q = append(q, []int{j, dir})
212+
}
213+
}
214+
}
215+
ans++
216+
}
217+
return -1
218+
}
71219
```
72220

73221
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
public:
3+
const int N = 6010;
4+
5+
int minimumJumps(vector<int>& forbidden, int a, int b, int x) {
6+
unordered_set<int> s(forbidden.begin(), forbidden.end());
7+
queue<vector<int>> q;
8+
q.push({0, 0});
9+
vector<vector<bool>> vis(N, vector<bool>(2));
10+
vis[0][0] = true;
11+
vis[0][1] = true;
12+
int ans = 0;
13+
while (!q.empty())
14+
{
15+
for (int t = q.size(); t; --t)
16+
{
17+
auto p = q.front();
18+
q.pop();
19+
int i = p[0], dir = p[1];
20+
if (i == x) return ans;
21+
vector<vector<int>> nxt;
22+
nxt.push_back({i + a, 1});
23+
if (dir) nxt.push_back({i - b, 0});
24+
for (auto& e : nxt)
25+
{
26+
int j = e[0];
27+
dir = e[1];
28+
if (j >= 0 && j < N && !s.count(j) && !vis[j][dir])
29+
{
30+
vis[j][dir] = true;
31+
q.push({j, dir});
32+
}
33+
}
34+
}
35+
++ans;
36+
}
37+
return -1;
38+
}
39+
};

0 commit comments

Comments
 (0)