Easy to grasp DP C++ solution


#1
int Solution::minPathSum(vector<vector<int> > &A) {
    int N = A.size(); //rows
    if(N == 0) return 0;
    int M = A[0].size(); //cols
    
    int dp[N][M]; memset(dp, 0, sizeof dp);
    
    for(int i = 0; i<N; i++){
        for(int j = 0; j<M; j++){
            dp[i][j] += A[i][j];
            if(i > 0 && j > 0) dp[i][j] += min(dp[i-1][j], dp[i][j-1]); // left and top
            else if(i>0) dp[i][j] += dp[i-1][j]; //for 1st row 0th col ele
            else if(j>0) dp[i][j] += dp[i][j-1]; // for 1st col 0th row ele
        }
    }
    
    return dp[N-1][M-1];
}