Skip to content

Commit

Permalink
Minimum number of jumps.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashish-kumar7 committed May 2, 2020
1 parent ae33f79 commit 3e09427
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions DP/Minimum number of jumps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<bits/stdc++.h>
using namespace std;
int jumping(int A[],int n){
if(n==0 || A[0]==0){
return -1;
}
int jump[n];
jump[0]=0;
for(int i=1;i<n;i++){
jump[i]=INT_MAX;
for(int j=0;j<i;j++){
if(i<=j+A[j]){
jump[i]=min(jump[i],jump[j]+1);
}
}
}
if(jump[n-1]==INT_MAX){
return -1;
}
else{
return jump[n-1];
}
}
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int A[n];
for(int i=0;i<n;i++){
cin>>A[i];
}
int l=jumping(A,n);
cout<<l<<endl;
}
return 0;
}

0 comments on commit 3e09427

Please sign in to comment.