Space O(n) DP Time: O(mn)


  • 0
    public class Solution {
        public int uniquePaths(int m, int n) {
            int[] cols = new int[n];
            Arrays.fill(cols, 1);
            for (int i = 1; i < m; i++) {
                for (int j = 1; j < n; j++) {
                    if (j == 1) {
                        cols[1] = cols[1] + 1;
                    } else {
                        cols[j] = cols[j] + cols[j - 1];
                    }
                }
            }
            return cols[n - 1];
        }
    }
    

Log in to reply
 

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