DP solution with O(n) space complexity


  • 1
    A
    public int uniquePaths(int m, int n) {
        if (m <= 0 || n <= 0) {
            return 0;
        }
        int[] counts = new int[n];
        counts[0] = 1;
        /*transitional function:  counts(i, j) = counts(i, j-1) + counts(i-1, j)*/
        for (int i = 0; i < m; i++) {
            for (int j = 1; j < n; j++) {
                counts[j] = counts[j - 1] + counts[j];
            }
        }
        return counts[n - 1];
    }

Log in to reply
 

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