Please solve the problem

Revision en1, by anshu2002, 2022-01-06 12:47:13
class Solution{
  public:
    int solve(int i,int arr[],vector<int>& dp)
    {
        //cout<<i<<"\n";
        if(i==1)
            return 0;
        if(dp[i]!=-1)
            return dp[i];
        int ans=1000001;
        for(int k=1;k<=arr[i-1] && i-k>=1;k++)
        {
            ans=min(ans,solve(i-k,arr,dp)+1);
        }
        return dp[i]=ans;
    }
    int minJumps(int arr[], int n){
        vector<int>dp(n+1,-1);
        reverse(arr,arr+n);
        int ans=solve(n,arr,dp);
        if(ans==1000001)
            ans=-1;
        return ans;
    }
};

I was solving the problem of minimum number of jumps to reach to the end of an array in geeksforgeeks Problem link

And this was my solution to it is right at the top

At worst , this should show TLE , but showing Segmentation fault . Can someone identify where am I wrong

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English anshu2002 2022-01-06 12:47:13 990 Initial revision (published)