-
Notifications
You must be signed in to change notification settings - Fork 0
/
322.零钱兑换.js
47 lines (39 loc) · 931 Bytes
/
322.零钱兑换.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
/*
* @lc app=leetcode.cn id=322 lang=javascript
*
* [322] 零钱兑换
*/
const { map } = require('async')
// @lc code=start
/**
* @param {number[]} coins
* @param {number} amount
* @return {number}
*/
var coinChange = function(coins, amount) {
// if all use 1 to make the coin change, then you need at least n
let memo = new Map()
return dp(coins, amount)
function dp(coins, amount) {
// base case
if (amount == 0) return 0
if (amount < 0) return -1
if (memo.get(amount)) {
return memo.get(amount)
}
let res = Infinity
for (let i = 0; i < coins.length; i++) {
const c = coins[i]
const subProblem = dp(coins, amount - c)
if (subProblem == -1) {
continue
}
// +1
res = Math.min(res, subProblem + 1)
}
memo.set(amount, res == Infinity ? -1 : res)
return memo.get(amount)
}
}
coinChange([1, 2, 5], 11)
// @lc code=end