-
Notifications
You must be signed in to change notification settings - Fork 101
/
change-making.ts
44 lines (35 loc) · 1.45 KB
/
change-making.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { range } from '../../utils';
/**
* Problem: find the minimum number of coins (of certain denominations) that add
* up to a given amount of money.
* @url https://en.wikipedia.org/wiki/Change-making_problem
*
* The solution uses dynamic-programming with top-down strategy instead of bottom-up
* to avoid resolution of every sub-problem, which is not needed for this problem.
* @url https://en.wikipedia.org/wiki/Dynamic_programming
*/
/**
* Return the minimum of coins that add up to a given amount of money.
* Time complexity: O(m*n) where m = coins.length, n = amount
* @param coins The available coins
* @param amount The value to add up to
*/
export function changeMaking(coins: number[], amount: number): number {
// Memoization array used to store best results for problems up to `amount` dimension
const results: number[] = new Array(amount + 1);
results.fill(-Infinity);
// Zero coins are needed for amount 0
results[0] = 0;
// Only one coin is needed if the amount is equal to one of the coins
coins.forEach(coin => (results[coin] = 1));
return minCoins(coins, results, amount);
}
function minCoins(coins: number[], results: number[], amount: number): number {
if (results[amount] >= 0) return results[amount];
let result: number = Infinity;
coins.filter(coin => coin <= amount).forEach(coin => {
result = Math.min(result, 1 + minCoins(coins, results, amount - coin));
});
results[amount] = result;
return result;
}