Skip to content

Commit 287029b

Browse files
committed
feat(algorithms):62 accepted && testing
1 parent 59c0df0 commit 287029b

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as asserts from "https://deno.land/std/testing/asserts.ts";
2+
import * as log from "https://deno.land/std/log/mod.ts";
3+
import { uniquePaths } from "./index.ts";
4+
5+
log.info("62. Unique Paths");
6+
7+
Deno.test({
8+
name: `
9+
Input: 3, 2
10+
Output: 3
11+
`,
12+
fn(): void {
13+
const result: number = uniquePaths(3, 2);
14+
asserts.assertEquals(3, result);
15+
},
16+
});
17+
18+
Deno.test({
19+
name: `
20+
Input: 7, 3
21+
Output: 28
22+
`,
23+
fn(): void {
24+
const result: number = uniquePaths(7, 3);
25+
asserts.assertEquals(28, result);
26+
},
27+
});
28+
29+
Deno.test({
30+
name: `
31+
Input: 3, 3
32+
Output: 6
33+
`,
34+
fn(): void {
35+
const result: number = uniquePaths(3, 3);
36+
asserts.assertEquals(6, result);
37+
},
38+
});
39+
40+
Deno.test({
41+
name: `
42+
Input: 1, 1
43+
Output: 1
44+
`,
45+
fn(): void {
46+
const result: number = uniquePaths(1, 1);
47+
asserts.assertEquals(1, result);
48+
},
49+
});
50+
51+
Deno.test({
52+
name: `
53+
Input: 2, 3
54+
Output: 3
55+
`,
56+
fn(): void {
57+
const result: number = uniquePaths(2, 3);
58+
asserts.assertEquals(3, result);
59+
},
60+
});
61+
62+
// 测试 16 * 16 的情况下 Runtime 耗时
63+
Deno.test({
64+
name: `
65+
Input: 16, 16
66+
Output: 155117520
67+
`,
68+
fn(): void {
69+
const result: number = uniquePaths(16, 16);
70+
asserts.assertEquals(155117520, result);
71+
},
72+
});

algorithms/0062.unique-paths/index.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* # 62. Unique Paths
3+
*
4+
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
5+
*
6+
* The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
7+
*
8+
* How many possible unique paths are there?
9+
*
10+
* ## Example
11+
*
12+
* ```bash
13+
* Input: m = 3, n = 2
14+
* Output: 3
15+
* Explanation:
16+
* From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
17+
* 1. Right -> Right -> Down
18+
* 2. Right -> Down -> Right
19+
* 3. Down -> Right -> Right
20+
* ```
21+
* ```bash
22+
* Input: m = 7, n = 3
23+
* Output: 28
24+
* ```
25+
*
26+
* ## Constraints
27+
*
28+
* - `1 <= m, n <= 100`
29+
* - It's guaranteed that the answer will be less than or equal to `2 * 10 ^ 9`.
30+
*/
31+
export type Solution = (m: number, n: number) => number;
32+
33+
/**
34+
* 动态规划
35+
* [i,j] -> [m-1,n-1] to [0,0];缓存每个点位的路径数量
36+
* @date 2020/07/07 08:47:49
37+
* @time O(2n)
38+
* @space O(1)
39+
* @runtime
40+
* @memory
41+
* @runtime_cn 72 ms, faster than 75.00%
42+
* @memory_cn 35.8 MB, less than 100.00%
43+
*/
44+
export const uniquePaths = (m: number, n: number): number => {
45+
const arr = new Array(m);
46+
for (let i = m - 1; i >= 0; i--) {
47+
arr[i] = new Array(n);
48+
for (let j = n - 1; j >= 0; j--) {
49+
if (i === m - 1 || j === n - 1) {
50+
arr[i][j] = 1;
51+
} else {
52+
arr[i][j] = arr[i + 1][j] + arr[i][j + 1];
53+
}
54+
}
55+
}
56+
return arr[0][0];
57+
};

0 commit comments

Comments
 (0)