forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHC-kang.ts
More file actions
65 lines (59 loc) · 1.52 KB
/
HC-kang.ts
File metadata and controls
65 lines (59 loc) · 1.52 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
/**
* Solution 1. recursive - failed with Stack Overflow
*/
function uniquePaths(m: number, n: number): number {
function factorialMemo() {
const cache = [0, 1];
return function factorial(n: number) {
if (cache[n]) return cache[n];
cache[n] = n * factorial(n - 1);
return cache[n];
};
}
const factorial = factorialMemo();
const total = m + n - 2;
const right = m - 1;
return Math.round(
factorial(total) / (factorial(right) * factorial(total - right))
);
}
/**
* Solution 2. for loop (with some 야매.. but it works)
* https://leetcode.com/problems/unique-paths
* T.C. O(m + n)
* S.C. O(m + n)
*/
function uniquePaths(m: number, n: number): number {
function factorialMemo() {
const cache = [1, 1];
return function factorial(n: number) {
if (cache[n]) return cache[n];
let result = cache[cache.length - 1];
for (let i = cache.length; i <= n; i++) {
result = result * i;
cache[i] = result;
}
return result;
};
}
const factorial = factorialMemo();
const total = m + n - 2;
const right = m - 1;
return Math.round(
factorial(total) / (factorial(right) * factorial(total - right))
);
}
/**
* Solution 3. DP
* T.C. O(m * n)
* S.C. O(m * n)
*/
function uniquePaths(m: number, n: number): number {
const dp: number[][] = Array.from({ length: m }, () => Array(n).fill(1));
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}