-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.ts
50 lines (44 loc) · 931 Bytes
/
fibonacci.ts
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
// What is a fibonacci?
// f(0) = 0
// f(1) = 1
// f(n) = f(n-1) + f(n-2)
// Solution one. Simple.
let i = 0;
function fibonacci(index: number): number {
if (index === 0) {
return 0;
}
if (index === 1) {
return 1;
}
i++;
// console.log(index);
return fibonacci(index - 1) + fibonacci(index - 2);
}
// console.log(fibonacci(9));
// console.log({ i });
// Solution two. Memoized.
type MapNumberNumber = { [a: number]: number };
let memo: MapNumberNumber = {};
let j = 0;
function optimizedFib(index: number): number {
if (index === 0) {
memo[0] = 0;
return 0;
}
if (index === 1) {
memo[1] = 1;
return 1;
}
// console.log(index);
if (memo[index] != null) {
return memo[index];
} else {
j++;
const result = optimizedFib(index - 1) + optimizedFib(index - 2);
memo[index] = result;
return result;
}
}
console.log(optimizedFib(9));
console.log({ j, memo });