We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functiongetWays(n,c){// Initialize an array called 'ways' of length 'n + 1' and fill it with 0s.letways=newArray(n+1).fill(0);// Set the number of ways to make change for the amount 0 to 1.ways[0]=1;// Iterate through each coin denomination in the array 'c'.for(letcoinofc){// For each coin denomination, iterate through each amount from 'coin' to 'n'.for(letamount=coin;amount<=n;amount++){// Update the number of ways to make change for the current 'amount'// by adding the number of ways to make change for 'amount - coin'// (i.e., the current amount minus the current coin denomination).ways[amount]+=ways[amount-coin];}}// Return the number of ways to make change for the target amount 'n'.returnways[n];}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Coin Change Problem
You are viewing a single comment's thread. Return to all comments →