-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicAlgorithms.js
More file actions
83 lines (69 loc) · 1.75 KB
/
Copy pathDynamicAlgorithms.js
File metadata and controls
83 lines (69 loc) · 1.75 KB
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
// DYNAMIC PROGRAMMING
// Cache values to avoid repeated calculations
// Top- Down Memoization & Bottom-Up Iterative
// Memoization + Recursive Approach
// memo-example
const cache = {};
const coins = [10, 6, 1];
const makeChange = (c) => {
// Return the value if it’s in the cache
if (cache[c]) return cache[c];
let minCoins = -1;
// Find the best coin
coins.forEach(coin => {
if (c - coin >= 0) {
let currMinCoins = makeChange(c - coin);
if (minCoins === -1 || currMinCoins < minCoins) {
minCoins = currMinCoins
}
}
})
// Save the value into the cache
cache[c] = minCoins + 1;
return cache[c];
}
console.log(makeChange(12)); //2
// --------------------
// factorial
// Task: convert this top-down recursive solution from a previous exercise to a bottom-up iterative solution
const memoize = (fn) => {
let cache = {};
return (...args) => {
let n = args[0];
if (n in cache) {
console.log('Fetching from cache', n);
return cache[n];
}
else {
console.log('Calculating result', n);
let result = fn(n);
cache[n] = result;
return result;
}
};
};
const factorial = memoize(
(x) => {
if (x === 0) {
return 1;
}
else {
return x * factorial(x - 1);
}
}
);
console.log(factorial(5)); // calculated
console.log(factorial(6)); // calculated for 6 and cached for 5
/*
output
Calculating result 5
Calculating result 4
Calculating result 3
Calculating result 2
Calculating result 1
Calculating result 0
120
Calculating result 6
Fetching from cache 5
720
*/