What's wrong with my code!


#1

It shows the following runtime error: Your submission stopped because of a runtime error. Ex: division by zero, array index out of bounds, uncaught exception You can try testing your code with custom input and try putting debug statements in your code.
Your submission encountered runtime error for the following input:
My code :
int Solution::uniquePaths(int A, int B) {
vector< vector < int > > V ;
int i, j;
for( i = 0; i <A ; i++){
for( j =0 ; j< B ; j++){
V[i][j] = 0;
}
}
for ( i = 0 ; i< A ; i++){
V[i][0] = 1;
}
for ( j = 0 ; j< B ; j++){
V[0][j] = 1;
}
for ( i = 1 ; i< A ; i++){
for( j = 1 ; i<B; j++){
V[i][j] = V[i-1][j] + V[i][j-1];
}
}

return V[A-1][B-1];
}


#2

You must initialize your vector V before accessing it with V[i][j]. vector<vector> V(A, vector(B)) ; This will also initialize all values with 0, so you don’t need the first nested for loops. You can also initialize them with 1 (like so vector<vector>V(A, vector(B,1)) , then you don’t need the second pair of nested for loops.


#3

I believe there exists a logical error. I don’t think taking the gcd of the list would do well for you. Try to think if lcm fixes your code?


#4

Try to use space efficient approach,
It can be solve with Space complexity:O(B) instead of O(A*B);
i.e. use 1 D array than change values accordingly.