My algorithm seems to slow Can anyone help me to optimize it?


  • 0
    D

    int uniquePaths(int m, int n) {

    //using combination
    //C((a+b)|Min(a,b))
    int a = m-1;
    int b = n-1;
    if(b==0 || a==0)
        return 1;
    int start = a + b;
    long long count1 = 1;
    long long count2 = 1;
    int total = (a<=b?a:b);
    for(int i = 0;i<total;++i)
    {
        count2 = count2 * (i+1);
        count1 = count1 * (start - i);
    }
    return count1 / count2;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.