-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic.js
More file actions
40 lines (35 loc) · 982 Bytes
/
dynamic.js
File metadata and controls
40 lines (35 loc) · 982 Bytes
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
//start with a problem that can be solved with dynamic programming and use recursion to solve
//then refactor it to use dynamic programming principles
//Fibonacci sequence
const fibonacci = (num) => {
if(num < 3) return 1;
let twoBack = 1;
let oneBack = 1;
let sum = 0;
const fibIt = (start) => {
sum = (twoBack + oneBack);
if(start === num) return sum;
twoBack = oneBack;
oneBack = sum;
fibIt(start+1);
}
fibIt(2);
return sum;
}
//Shorter recursive fibonacci (Not as efficient, though)
const fibShort = (num) => {
if(num < 2) return 1;
return fibShort(num -1) + fibShort(num-2);
}
//recursive fibonacci function that takes advantage of memoization
const fibMemo = (n, memo=[]) => {
console.log("here")
if(memo[n] !== undefined) return memo[n];
if(n <= 2) return 1;
let res = fibMemo(n-1, memo) + fibMemo(n-2, memo);
memo[n] = res;
return res;
}
console.log(fibShort(10))
console.log(fibonacci(10))
console.log(fibMemo(10));