-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMinimumJumpsToReachHome.java
More file actions
73 lines (57 loc) · 2.21 KB
/
MinimumJumpsToReachHome.java
File metadata and controls
73 lines (57 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package graph;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
/**
* Description: https://leetcode.com/problems/minimum-jumps-to-reach-home
* Difficulty: Medium
* Time complexity: O(n)
* Space complexity: O(n)
*/
public class MinimumJumpsToReachHome {
private static final int START = 0;
private static final int LIMIT = 6000; // too much math involved to prove
private static final int BACKWARD = 0;
private static final int FORWARD = 1;
private static final int UNDEFINED = -1;
public int minimumJumps(int[] forbidden, int a, int b, int x) {
Set<Integer> forbiddenSet = new HashSet<>();
for (int f : forbidden) {
forbiddenSet.add(f);
}
return findShortestPath(x, a, b, forbiddenSet);
}
private int findShortestPath(int target, int forward, int backward, Set<Integer> forbidden) {
Set<Node> visited = new HashSet<>();
visited.add(new Node(START, BACKWARD));
visited.add(new Node(START, FORWARD));
Queue<Node> planned = new LinkedList<>();
planned.offer(new Node(START, UNDEFINED));
int steps = 0;
while (!planned.isEmpty()) {
int levelSize = planned.size();
for (int i = 0; i < levelSize; i++) {
Node current = planned.poll();
if (current.idx == target) return steps;
Node moveForward = new Node(current.idx + forward, FORWARD);
Node moveBackward = new Node(current.idx - backward, BACKWARD);
if (!forbidden.contains(moveForward.idx)
&& moveForward.idx <= LIMIT
&& visited.add(moveForward)) {
planned.offer(moveForward);
}
if (current.direction != BACKWARD // can't jump backward twice
&& !forbidden.contains(moveBackward.idx)
&& moveBackward.idx >= 0
&& visited.add(moveBackward)) {
planned.offer(moveBackward);
}
}
steps++;
}
return -1;
}
private record Node(int idx, int direction) {
}
}