-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
85 lines (83 loc) · 2.1 KB
/
index.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
81
82
83
84
85
/*
* @lc app=leetcode id=322 lang=javascript
*
* [322] Coin Change
*
* https://leetcode.com/problems/coin-change/description/
*
* algorithms
* Medium (28.75%)
* Total Accepted: 161.9K
* Total Submissions: 558.9K
* Testcase Example: '[1,2,5]\n11'
*
* 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.
*
*/
/**
* 思路:
*
* 这是一道动态规划的题目
* 设 dp[n] 为第价格为 n 的最优组合
* dp[0] = 0
*
* dp[1] = conins.have(1)? 1 : -1;
* dp[2] = conins.have(2)? 1 : (conins.have(1) ? 1 + dp[1] : -1)
*
* dp[n] = conins.have(n)? 1 :
* 遍历 coins ,dp[n] = Min( 1 + dp[n - conin]),此时dp[n - conin]必须有解才能用无解则不能用
* n - conin如果 小于0则可以直接判断无解
*
* @param {number[]} coins
* @param {number} amount
* @return {number}
*/
var coinChange = function(coins, amount) {
const dp = [0];
coins.sort((a, b) => a - b);
for (let i = 1; i <= amount; i++) {
for (let j = coins.length - 1; j >=0; j--) {
const num = i - coins[j];
if (num === 0) {
dp[i] = 1;
j = -1;
continue;
}
if (num > 0 && dp[num] > 0) {
dp[i] = dp[i] === undefined ? 1 + dp[num] : Math.min(1 + dp[num], dp[i]);
continue;
}
}
if (dp[i] === undefined) {
dp[i] = -1;
}
}
return dp[amount];
};
console.log(coinChange([186,419,83,408], 6249));
module.exports = {
id:'322',
title:'Coin Change',
url:'https://leetcode.com/problems/coin-change/description/',
difficulty:'Medium',
}