-
Notifications
You must be signed in to change notification settings - Fork 0
/
dp.js
99 lines (73 loc) · 2.36 KB
/
dp.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
86
87
88
89
90
91
92
93
94
95
96
97
// -----------------------------------------
// Chapter 17
// Dynamic programming
// -----------------------------------------
function change(coins, amount) {
const MAX = Number.MAX_SAFE_INTEGER
let solutions = []
solutions[0] = [0]
// for zero coins there is no solution (set to max int)
for (let i = 1; i <= amount; ++i) {
solutions[0][i] = MAX
}
// no coins are needed to pay a zero amount
for (let j = 1; j <= coins.length; ++j) {
solutions[j] = [0]
}
// find all sub-solutions
// each coin
for (let i = 1; i <= coins.length; ++i) {
// for amounts smaller than the current coin discard this coin
for (let j = 0; j < coins[i-1]; ++j) {
solutions[i][j] = solutions[i-1][j]
}
// for amounts greater than the current coin either use the largest coin or discard it
for (let j = coins[i-1]; j <= amount; ++j) {
const remainder = j - coins[i-1]
solutions[i][j] = Math.min(solutions[i][remainder] + 1, solutions[i-1][j])
}
}
// console.log(solutions)
// give the final solution for our amount
return solutions[coins.length][amount]
}
// console.log(change([1, 3, 4], 6))
// optimize memory, only use the previous row
function change2(coins, amount) {
let solutions = [0]
for (let i = 1; i <= amount; ++i) {
solutions[i] = Number.MAX_SAFE_INTEGER
}
for (let i = 0; i <= coins.length; ++i) {
for (let j = coins[i-1]; j <= amount; ++j) {
solutions[j] = Math.min(
solutions[j - coins[i-1]] + 1,
solutions[j]
)
}
}
return solutions[amount]
}
// console.log(change2([1, 3, 4], 6))
function frog(distances, position) {
let dp = []
// starting position
dp[0] = 1
// prefill
for (let i=1; i<=position; ++i) {
dp[i] = 0
}
for (let j=1; j <= position; ++j) {
console.log('position', j)
for (let i=0; i < distances.length; ++i) {
if (distances[i] <= j) {
dp[j] = (dp[j] + dp[j - distances[i]])
}
console.log('d', distances[i], dp.join(' '))
}
}
return dp[position]
}
// console.log(frog([3,5], 10)) // only one way to jump, 5 + 5
// console.log(frog([3], 10)) // nope
console.log(frog([2, 5], 10)) // 2 ways, 5x2 or 2x5