Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
557 B
14
Indexable
Never
// change coins using array, tabulation (for optimization)

const coins = [3,5,10,15];
const amount = 30;
const dic = {};

function changeCoins(amount, start, end) {
  const temp = JSON.stringify([amount, start, end]);
  
  if (temp in dic) return dic[temp];
  
  if (start === end || amount < 0) return 0;
  
  if (amount === 0) return 1;
  
  const variation1 = change(amount - coins[start], start, end);
  const variation2 = change(amount, start + 1, end);
  dic[temp] = a + b;
  
  return dic[temp];
}

const changedCoins = changeCoins(amount, 0, 3);