-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic-programming.ts
40 lines (35 loc) · 935 Bytes
/
dynamic-programming.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
function recursiveFib(n: number): number {
if (n <= 1) {
return n;
}
return recursiveFib(n - 1) + recursiveFib(n - 2);
}
function dynamicFib(n: number): number {
let a = 0;
let b = 1;
for (let i = 0; i < n; i++) {
[a, b] = [a + b, a];
}
return a;
}
function recursiveNumPaths(height: number, width: number): number {
if (height === 0 || width === 0) {
return 1;
}
return (
recursiveNumPaths(height, width - 1) + recursiveNumPaths(height - 1, width)
);
}
function dynamicNumPaths(height: number, width: number) {
const memo: number[][] = Array.from(Array(height + 1)).map(() =>
Array(width + 1).fill(1)
);
for (let i = 1; i < memo.length; i++) {
const row = memo[i];
for (let j = 1; j < row.length; j++) {
memo[i][j] = memo[i - 1][j] + memo[i][j - 1];
}
}
return memo[height][width];
}
export { recursiveFib, dynamicFib, recursiveNumPaths, dynamicNumPaths };