2ms C++ O(mn) time and O(min(m, n)) space


  • 0
    L
    int uniquePaths(int m, int n) {
        int **opt = new int*[2], i;
        int minv=min(m, n), maxv=max(m, n);
        for (i=0; i<2; i++)
        {
            opt[i] = new int[minv];
        }
        for (i=0; i<minv; i++) opt[0][i]=1;
        opt[1][0]=1;
        for (i=1; i<maxv; i++)
        {
            for (int j=1; j<minv; j++)
            {
                opt[i%2][j]=opt[(i+1)%2][j]+opt[i%2][j-1];
            }
        }
        return opt[(i+1)%2][minv-1];
    }

Log in to reply
 

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