A simple dp solution with O(n) space


  • 0
    B
    class Solution {
    public:
        int uniquePaths(int m, int n) {
                
            int dp[n];
            dp[n-1] = 1;
            for ( int i = 0; i < n-1; i++ )
                dp[i] = 0;
                
            for ( int i = m-1; i >= 0; i-- ) {
                for ( int j = n-2; j >= 0; j-- ) {
                    dp[j] = dp[j+1] + dp[j];
                }
            }
            return dp[0];
        }
    };

Log in to reply
 

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