• + 0 comments

    JS Javascript solution passes all tests:

    function getWays(n, c) {
        // Write your code here
        const dp = new Array(c.length).fill(new Array(n).fill(0));
        const sorted = c.sort((a,b)=>a-b);
        for (let i = 0; i<sorted.length; i++){
            for(let j=0; j<=n; j++){
            if (j === 0) {
                dp[i][j] = 1;
                continue;
            }
            if (i === 0){
                dp[i][j] = j%sorted[i] === 0 ? 1 : 0;
                continue;
            }
            if (sorted[i] > j) {
                dp[i][j] = dp[i-1][j];
            } else {
                dp[i][j] = dp[i][j-sorted[i]] + dp[i-1][j];
            }
            }
            if (sorted[i]>n) {
            return dp[i][n];
            }
        }
        return dp[sorted.length-1][n];
    }