forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYjason-K.ts
More file actions
29 lines (24 loc) ยท 838 Bytes
/
Yjason-K.ts
File metadata and controls
29 lines (24 loc) ยท 838 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
/**
* m * n ๊ทธ๋ฆฌ๋์์ ์ข์ธก ์๋จ์์ ์ฐ์ธก ํ๋จ๊น์ง ๊ฐ ์ ์๋ ๊ฒฝ๋ก์ ์
* @param {number} m - ๊ทธ๋ฆฌ๋์ ํ ๊ธธ์ด
* @param {number} n - ๊ทธ๋ฆฌ๋์ ์ด ๊ธธ์ด
* @returns {number} ์ฐ์ธก ํ๋จ๊น์ง ๊ฐ ์ ์๋ ๊ฒฝ์ฐ์ ์
*
* - ์๊ฐ ๋ณต์ก๋: O(m * n)
* - ์ ์ฒด ์
์ ํ ๋ฒ์ฉ ์ํ
*
* - ๊ณต๊ฐ ๋ณต์ก๋: O(n)
* - 1์ฐจ์ ๋ฐฐ์ด ์ฌ์ฉ (์ด ํฌ๊ธฐ๋งํผ์ ๋ฐฐ์ด)
*/
function uniquePaths(m: number, n: number): number {
// n(์ด) ํฌ๊ธฐ์ ๋ฐฐ์ด์ ์์ฑ
const dp = Array(n).fill(1);
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
// ํ์ฌ ๊ฐ = ์์ชฝ(dp[j]) + ์ผ์ชฝ(dp[j-1])
dp[j] = dp[j] + dp[j - 1];
}
}
// dp[n-1]์ ์ฐ์ธก ํ๋จ๊น์ง์ ๊ฒฝ๋ก ์๊ฐ ์ ์ฅ
return dp[n - 1];
}