forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdy8739.js
More file actions
72 lines (52 loc) ยท 1.42 KB
/
jdy8739.js
File metadata and controls
72 lines (52 loc) ยท 1.42 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
var uniquePaths = function (m, n) {
const cache = new Map();
const dfs = (row, col) => {
const cacheKey = `${row}-${col}`;
if (cache.has(cacheKey)) {
return cache.get(cacheKey);
}
if (row === m - 1 && col === n - 1) {
return 1;
}
let count = 0;
if (row < m - 1) {
count += dfs(row + 1, col);
}
if (col < n - 1) {
count += dfs(row, col + 1);
}
cache.set(cacheKey, count);
return count;
}
return dfs(0, 0);
};
// ์๊ฐ๋ณต์ก๋ O(m * n)
// ๊ณต๊ฐ๋ณต์ก๋ O(m * n) - 1 (matrix[m][n]์ ๋ํ ์บ์๋ ํฌํจ๋์ง ์์ผ๋ฏ๋ก)
var uniquePaths = function(m, n) {
const matrix = [];
for (let i=0; i<m; i++) {
const row = new Array(n).fill(1);
matrix.push(row);
}
for (let j=1; j<matrix.length; j++) {
for (let k=1; k<matrix[0].length; k++) {
matrix[j][k] = matrix[j - 1][k] + matrix[j][k - 1];
}
}
return matrix[m - 1][n - 1];
};
// ์๊ฐ๋ณต์ก๋ O(m * n)
// ๊ณต๊ฐ๋ณต์ก๋ O(m * n)
var uniquePaths = function(m, n) {
const row = new Array(n).fill(1);
for (let i=1; i<m; i++) {
let left = 1;
for (let j=1; j<n; j++) {
row[j] += left;
left = row[j];
}
}
return row[n - 1];
};
// ์๊ฐ๋ณต์ก๋ O(m * n)
// ๊ณต๊ฐ๋ณต์ก๋ (n)