forked from jeantimex/javascript-problems-and-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin-change.js
80 lines (71 loc) · 1.7 KB
/
coin-change.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Coin Change
*
* You are given coins of different denominations and a total amount of money amount.
* Write a function to compute the fewest number of coins that you need to make up that amount.
* If that amount of money cannot be made up by any combination of the coins, return -1.
*
* Example 1:
*
* Input: coins = [1, 2, 5], amount = 11
* Output: 3
* Explanation: 11 = 5 + 5 + 1
*
* Example 2:
*
* Input: coins = [2], amount = 3
* Output: -1
*
* Note:
* You may assume that you have an infinite number of each kind of coin.
*/
/**
* Recursion
*
* @param {number[]} coins
* @param {number} amount
* @return {number}
*/
const coinChange = (coins, amount) => {
// base case
if (amount == 0) {
return 0;
}
// initialize result
let res = Infinity;
// try every coin that has smaller value than amount
for (let i = 0; i < coins.length; i++) {
if (coins[i] <= amount) {
const count = coinChange(coins, amount - coins[i]);
if (count !== -1) {
res = Math.min(res, count + 1);
}
}
}
return res === Infinity ? -1 : res;
};
/**
* Dynamic Programming
*
* @param {number[]} coins
* @param {number} amount
* @return {number}
*/
const coinChange_DP = (coins, amount) => {
const dp = Array(amount + 1).fill(Infinity);
// base case
dp[0] = 0;
// try every coin that has smaller value than amount
for (let i = 1; i <= amount; i++) {
for (let j = 0; j < coins.length; j++) {
if (coins[j] <= i) {
const count = dp[i - coins[j]];
if (count !== Infinity) {
dp[i] = Math.min(dp[i], count + 1);
}
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
};
export { coinChange, coinChange_DP };