-
Notifications
You must be signed in to change notification settings - Fork 2
/
change.js
51 lines (45 loc) · 1.25 KB
/
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
// 找零
// 我们的零钱 1 5 10 20 50 100
// 怎么找钱最合适
class Change{
constructor(changeType){
this.changeType = changeType
this.cache = {}
}
makeChange(amount){
// 最少张数的一个数组
let min = []
if(!amount){
return []
}
if(this.cache[amount]){
return this.cache[amount]
}
for(let i=0;i<this.changeType.length;i++){
// 先找一张试试
const leftAmount = amount - this.changeType[i]
let newMin
if(leftAmount>=0){
// 只要还得继续找钱
// 下一步找钱的数组
newMin = this.makeChange(leftAmount)
}
if(leftAmount>=0 && (newMin.length<min.length-1|| !min.length)){
// 纠正结果
min = [this.changeType[i]].concat(newMin)
}
}
return this.cache[amount] = min
}
}
// const change = new Change([1,5,10,20,50,100])
// console.log(change.makeChange(2))
// console.log(change.makeChange(5))
// console.log(change.makeChange(13))
// console.log(change.makeChange(16))
// console.log(change.makeChange(35))
// console.log(change.makeChange(383))
// 结论是慢慢优化出来的
const change = new Change([1, 3, 4])
console.log(change.makeChange(6)) // 其实33最好w
// 贪心给出的是411