Combinatorics DP Soln C++


#1
int Solution::uniquePaths(int A, int B) {
    //Total directions is always A+B-2 ie. (A-1) + (B-1) motions
    int N = A+B-2;
    int r = A-1; //rows
    //Now we do combinatorics
    // A+B-2 C r (instead of rows r, we can use columns c = B-1 also)
    double res = 1;
    for(int i = 1; i<=r; i++){
        //res = (res*(N-r+i)) / i;
        res = res * (N--)/i;
    }
    return (int)res;
}

Take U Forward’s code with my lil explanations