Getting rum time error


#1

int Solution::minPathSum(vector<vector > &A) {
// author : ninja
if(A.size() == 0 || A[0].size() == 0) return 0;

int R = A.size();
int C = A[0].size();
for(int i=0;i<R+1;i++) {
    for(int j=0;j<C+1;j++) {
        if(i >= 0 && j >= 0) A[i][j] += min(A[i-1][j], A[i][j-1]);
        else if(i >= 0) A[i][j] += A[i-1][j];
        else if(j >= 0) A[i][j] += A[i][j-1];
    }
}

return A[R][C];

}


#2

Hello @shubhankur-kumar,

The runtime error is there since your i and j are bound to R and C, i.e., to the indices where the array A does not exist. Hence, in reality, you get a SEGFAULT, which the IB’s console shows as the runtime error.

BTW, one thing I’d like to point out is that it would be better to use another array to store the distances instead of messing up the original array. Try dry-running your code for a small 2D array and you will realise why :wink:

Best