Error- Segmentation


#1

Dont know why it is not working

vector<vector > Solution::solve(int A, vector<vector > &B) {
int N=B.size();
if(N==0)
return {};
int M=B[0].size();
if(M==0)
return {};
int dp[N][M][A+1];
for(int k=0;k<=A;k++)
{
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{

                    dp[i][j][k]=B[i][j];
               if(k==0) continue;
               dp[i][j][k]=dp[i][j][k-1];
               
                if(i>0)
                dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-1]);
                if(i<N-1)
                dp[i][j][k]=max(dp[i][j][k],dp[i+1][j][k-1]);
                if(j>0)
                dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-1]);
                if(j<M-1)
                dp[i][j][k]=max(dp[i][j][k],dp[i][j+1][k-1]);
        }
    }
            }
            
    vector<vector<int> >ANS(N,vector<int>(M));
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<M;j++)
        {
            ANS[i][j]=dp[i][j][A];
        }
    }
    
    //for(int i=0;i<N;i++)
return ANS;

}


#2

if(!you_have_done)
They aren’t allowing space complexity to be nmk. Since we rely on previous k-1th instance, we don’t need to store 0…k-2th instances.