anshu2002's blog

By anshu2002, history, 13 months ago, In English
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

 
 
 
 
  • Vote: I like it
  • +7
  • Vote: I do not like it

| Write comment?
»
13 months ago, # |
  Vote: I like it +3 Vote: I do not like it

Maybe your code exceeds the stack limit of gfg. You can see this and this blogs related to the same problem.

»
13 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Every time when you call method "solve", you make a copy of ur array arr[] annd give it to parameter. You can give pointer of this array instead of making copy