Source: https://leetcode.com/problems/unique-paths/

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner. The test cases are generated so that the answer will be less than or equal to 2 * 109.

/**
* @param {number} m
* @param {number} n
* @return {number}
*/
// Within bounds if 0 <= row < m, 0 <= col < n
// Reached goal if row === m-1 and col === n-1
// At each position in the grid you can only make 2 choices: going down or right
// To go down we change row + 1, col
// To go right we change row, col + 1
// Recursive Approach
// Base Cases
// If not within bounds, return 0
// If row === m-1 and col === n - 1, return 1
// Recursively try going down + try going right
// O(2^(m+n)) time; O(m+n) space for recursive stack
var uniquePaths = function(m, n) {
const withinBounds = (row, col) => {
return (0 <= row && row < m && 0 <= col && col < n);
};
const reachedTarget = (row, col) => {
return (row === m - 1 && col === n - 1);
};
const rec = (row, col) => {
if (!withinBounds(row, col)) {
return 0;
}
if (reachedTarget(row, col)) {
return 1;
}
// Recursively try going down and going right
const downPaths = rec(row + 1, col);
const rightPaths = rec(row, col + 1);
return downPaths + rightPaths;
};
return rec(0, 0);
};