• + 0 comments

    JS Solution, using Dynamic Programming, passes all test cases

    /*
     * Complete the 'solve' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER n
     *  2. INTEGER m
     */
    const MOD = 1E9 + 7;
    function solve(n, m) {
        // define the matrix
        const memo = new Array(n+1).fill(null).map(el => new Array(m+1).fill(0));
        // first row
        for (let i=1; i<=m; i++) memo[0][i] = 1;
        // first col
        for (let i=1; i<=n; i++) memo[i][0] = 1;
        
        // now the grid
        for(let i = 1; i <= n; i++) {
            for (let j = 1; j <= m; j++) {
                memo[i][j] = (memo[i][j-1] + memo[i-1][j]) % MOD;
            }
        }
        // this is our answer
        return memo[n][m]
    }