Easy memoization approach in C++


#1
int dp[101][101];
int helper(vector<vector<int>>&A,int row,int col)
{
    if(row<0 || col<0 || row>=A.size() || col>=A[0].size() || A[row][col]>0)
    {
        return dp[row][col]=0;
    }
    if(row==A.size()-1 && col==A[0].size()-1)
    {
        return dp[row][col]=1;
    }
    if(dp[row][col]!=-1)
    {
        return dp[row][col];
    }
    A[row][col]=2;
    if(dp[row+1][col]==-1)
    {
        dp[row+1][col]=helper(A,row+1,col);
    }
    if(dp[row][col+1]==-1)
    {
        dp[row][col+1]=helper(A,row,col+1);
    }
    int x=dp[row+1][col]+dp[row][col+1];
    A[row][col]=0;
    return dp[row][col]=x;
}

int Solution::uniquePathsWithObstacles(vector<vector<int> > &A) 
{
    memset(dp,-1,sizeof(dp));
    return helper(A,0,0);
}