0ms C++ solution using combination number


  • 0
    S

    For example, if the grid is 3 by 7, we need move to the right 6 times, and move down 2 times. In other words, we have 8 operations, and 2 of them are going down, the rest of them are going right. The result should be choose n-1 from m+n-2

    class Solution {
    public:
        int choose(double a, double b){
            double ans = 1;
            while(b){
                ans *= a-- / b--;
            }
            return ans + 0.5;
        }
        int uniquePaths(int m, int n) {
            return choose(m+n-2,n-1);
        }
    };
    

Log in to reply
 

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