Блог пользователя anshu2002

Автор anshu2002, история, 2 года назад, По-английски
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

  • Проголосовать: нравится
  • +7
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

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

»
2 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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