C++ dp solution, O(n) space


  • 0
    I
    int uniquePaths(int m, int n) {
        vector<vector<int>> dp(2, vector<int>(n, 1));
        int idx = 1;
        for (int i = 1; i < m; i++, idx = 1 - idx) {
            for (int j = 1; j < n; j++) {
                dp[idx][j] = dp[idx][j - 1] + dp[1 - idx][j];
            }
        }
        return dp[1 - idx][n - 1];
    }

Log in to reply
 

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