-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1345.cpp
50 lines (49 loc) · 1.41 KB
/
1345.cpp
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
class Solution {
public:
int minJumps(vector<int>& arr) {
unordered_map<int,vector<int>>mp;
if(arr.size()==1){
return 0;
}
for(int i=1;i<arr.size();i++){
mp[arr[i]].push_back(i);
}
int visited[arr.size()];
for(int k=0;k<arr.size();k++){
visited[k]=0;
}
queue<int>Q;
Q.push(0);
visited[0]=1;
int jump=0;
while(!Q.empty()){
int n=Q.size();
for(int i=0;i<n;i++){
int val=Q.front();
Q.pop();
if(val==arr.size()-1){
return jump;
}
if(val-1>=0 && val-1<arr.size() && visited[val-1]==0 ){
Q.push(val-1);
visited[val-1]=1;
}
if(val+1>=0 && val+1<arr.size() && visited[val+1]==0 ){
Q.push(val+1);
visited[val+1]=1;
}
if(mp.find(arr[val])!=mp.end()){
for(auto data:mp[arr[val]]){
if(visited[data]==0){
Q.push(data);
visited[data]=1;
}
}
}
mp[arr[val]].clear();
}
jump++;
}
return jump;
}
};