Very very simple C++ solution in 2 lines


#1

int Solution::uniquePaths(int A, int B) {
if(A==1 || B==1){
return 1;
}
return uniquePaths(A-1,B) + uniquePaths(A,B-1);
}


#2

why its working?? it should be like
if(A==1&&B==1) return 1;
if(A<=1||B<=1) return 0;
because going to first row,does not mean we reached the target cell… it has to be both index


#3

Very very very simple solution in Java or C++ :slight_smile:

 return (A == 1 || B == 1) ? 1 : uniquePaths(A - 1, B) + uniquePaths(A, B - 1);

#4

can you explain it?please


#5

but this very inefficient
interviewer will never accept it
try using dp