// A robot is located at the top-left corner of a m x n grid.

// The robot can only move either down or right at any point in time.
// The robot is trying to reach the bottom-right corner of the grid.

// How many possible unique paths are there?

// Note: m and n will be at most 100.


// Input: Integer, Integer
// ex. 2, 3
// ex. 3, 3

// Output: Integer
// ex. 3
// ex. 6


var uniquePaths = function(m, n) { // 112ms runtime
    if (m === 0 || n === 0) return 0; // when either is 0 there will be no way to go down or right, so return 0.
    if (m === 1 || n === 1) return 1; // when either is 1 there can only be one path, so return 1

    var dp = []; // dynamic programming
    for (var i = 0; i < n; i++) {
        dp[i] = 1;
    }
    for (i = 1; i < m; i++) {
        for (var j = 1; j < n; j++) {
            dp[j] += dp[j - 1];
        }
    }
    return dp[n - 1];
};

// --------------------------------------------


var uniquePaths = function(m, n) {
    var map = [];
    for(var i = 0; i < m; i++) {
        map[i] = [];
        for (var j = 0; j < n; j++) {
            map[i][j] = 0;
        }
    }
    for(i = 0; i<m; i++){ // When (n===0||m===0) the function always returns 1 since the robot can't go left or up.
        map[i][0] = 1;
    }
    for(j= 0; j<n; j++){ // Populate the edges with 1 first and use DP to get the full 2-D array.
        map[0][j] = 1;
    }
    for(i = 1; i<m; i++){
        for(j = 1; j<n; j++){
            map[i][j] = map[i-1][j]+map[i][j-1]; // For all other cells. The result = uniquePaths(m-1,n)+uniquePaths(m,n-1)
        }
    }
    return map[m-1][n-1];
};

@@ -372,16 +372,557 @@
  ex. 6%0A%0A
+var uniquePaths = function(m, n) %7B // 108ms runtime%0A    var tab = %5B%5D;%0A    for(var i = 0; i %3C m; i++) %7B // create 2d matrix%0A        tab%5Bi%5D = %5B%5D;%0A        for (var j = 0; j %3C n; j++) %7B%0A            tab%5Bi%5D%5Bj%5D = 0;%0A        %7D%0A    %7D%0A    for(i=0; i%3Cm; i++)%7B%0A        for(j=0; j%3Cn; j++)%7B%0A            if(i===0 %7C%7C j===0)%7B%0A                tab%5Bi%5D%5Bj%5D = 1;%0A            %7D%0A            else%7B%0A                tab%5Bi%5D%5Bj%5D = tab%5Bi-1%5D%5Bj%5D+tab%5Bi%5D%5Bj-1%5D;%0A            %7D%0A        %7D%0A    %7D%0A%0A    return tab%5Bm-1%5D%5Bn-1%5D;%0A%7D;%0A%0A// ---------------------------------------------------%0A
 %0Avar uni

add solution