Skip to content

Commit bb26128

Browse files
committed
implement: 322
1 parent aa5b8b8 commit bb26128

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
121121
|309|[Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) | [JavaScript](./src/best-time-to-buy-and-sell-stock-with-cooldown/res.js)|Medium|
122122
|310|[Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/) | [JavaScript](./src/minimum-height-trees/res.js)|Medium|
123123
|315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) <sup>*</sup> | [JavaScript](./src/count-of-smaller-numbers-after-self/res.js)|Hard|
124+
|322|[Coin Change](https://leetcode.com/problems/coin-change/) | [JavaScript](./src/coin-change/res.js)|Medium|
124125
|327|[Count of Range Sum](https://leetcode.com/problems/count-of-range-sum/) | [JavaScript](./src/count-of-range-sum/res.js)|Hard|
125126
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [JavaScript](./src/increasing-triplet-subsequence/res.js)|Medium|
126127
|340|[Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [JavaScript](./src/longest-substring-with-at-most-k-distinct-characters/res.js)|Hard|

‎src/coin-change/res.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @param {number[]} coins
3+
* @param {number} amount
4+
* @return {number}
5+
*/
6+
var coinChange = function(coins, amount) {
7+
if (!amount) return 0;
8+
const mem = [];
9+
for (let i = 0; i <= amount; i++) {
10+
mem.push(-2);
11+
}
12+
13+
const getMaxVal = function(cList, remain) {
14+
if (remain == 0) return 0;
15+
if (mem[remain] != -2) return mem[remain];
16+
let ans = Number.MAX_SAFE_INTEGER;
17+
18+
for (let i = 0; i < cList.length; i++) {
19+
const ele = cList[i];
20+
21+
if (remain - ele < 0) continue;
22+
const sub = getMaxVal(cList, remain-ele);
23+
24+
if (sub === -1) continue;
25+
ans = Math.min(ans, sub+1);
26+
}
27+
28+
mem[remain] = ans === Number.MAX_SAFE_INTEGER ? -1 : ans;
29+
return mem[remain];
30+
}
31+
32+
getMaxVal(coins, amount);
33+
34+
return mem[amount];
35+
};
36+
37+
/**
38+
* DP 解法二
39+
* @param {*} coins
40+
* @param {*} amount
41+
*/
42+
var coinChange = function(coins, amount) {
43+
if (!amount) return 0;
44+
const dp = [0];
45+
46+
for (let i = 0; i <= amount; i++) {
47+
i && dp.push(amount+1);
48+
for (let j = 0; j < coins.length; j++) {
49+
const coin = coins[j];
50+
51+
if (coin <= i) {
52+
dp[i] = Math.min(dp[i], dp[i-coin]+1);
53+
}
54+
}
55+
}
56+
57+
return dp[amount] > amount ? -1 : dp[amount];
58+
};

0 commit comments

Comments
 (0)