We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3d94752 commit 5f0e323Copy full SHA for 5f0e323
Medium/Jump Game III.java
@@ -0,0 +1,27 @@
1
+class Solution {
2
+ public boolean canReach(int[] arr, int start) {
3
+ Queue<Integer> queue = new LinkedList<>();
4
+ Set<Integer> visited = new HashSet<>();
5
+ queue.add(start);
6
+ while (!queue.isEmpty()) {
7
+ int size = queue.size();
8
+ for (int i = 0; i < size; i++) {
9
+ int removed = queue.remove();
10
+ if (arr[removed] == 0) {
11
+ return true;
12
+ }
13
+ if (visited.contains(removed)) {
14
+ continue;
15
16
+ visited.add(removed);
17
+ if (removed + arr[removed] < arr.length) {
18
+ queue.add(removed + arr[removed]);
19
20
+ if (removed - arr[removed] >= 0) {
21
+ queue.add(removed - arr[removed]);
22
23
24
25
+ return false;
26
27
+}
0 commit comments