-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathjumpGame.java
More file actions
24 lines (24 loc) · 737 Bytes
/
jumpGame.java
File metadata and controls
24 lines (24 loc) · 737 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public boolean canReach(int[] arr, int start) {
HashMap<Integer, Boolean>visited=new HashMap<Integer, Boolean>();
Queue<Integer>q=new LinkedList<Integer>();
q.add(start);
while(!q.isEmpty()){
if(arr[q.peek()]==0)
return true;
if(visited.get(q.peek())!=null){
q.poll();
continue;
}
if(arr[q.peek()]+q.peek()<arr.length){
q.add(arr[q.peek()]+q.peek());
}
if(q.peek()-arr[q.peek()]>=0){
q.add(q.peek()-arr[q.peek()]);
}
visited.put(q.peek(), true);
q.poll();
}
return false;
}
}