forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJumpGameIV.java
54 lines (44 loc) · 1.46 KB
/
JumpGameIV.java
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
class Solution {
// TC : O(n2)
public int minJumps(int[] arr) {
Map<Integer, List<Integer>> map = new HashMap<>();
for(int i=0;i<arr.length;i++){
List<Integer> indices = map.getOrDefault(arr[i], new ArrayList<>());
indices.add(i);
map.put(arr[i], indices);
}
Queue<Integer> qu = new LinkedList<>();
qu.offer(0);
boolean[] visited= new boolean[arr.length];
int level = 0;
while(!qu.isEmpty()){
int size = qu.size();
while(size-->0){
Integer head = qu.poll();
if(head == arr.length -1){
return level;
}
if(head <0 || head>=arr.length || visited[head]){
continue;
}
if(head - 1 >0 && !visited[head-1]) {
qu.offer(head-1);
}
if(head + 1 <arr.length && !visited[head+1]) {
qu.offer(head+1);
}
if(map.containsKey(arr[head])){
for(int index : map.get(arr[head])){
if(index>=0 && index<arr.length && !visited[index]){
qu.offer(index);
}
}
map.remove(arr[head]);
}
visited[head] = true;
}
level++;
}
return -1;
}
}