Skip to content

Commit 7fb7bec

Browse files
committed
https://leetcode.cn/problems/unique-paths-ii
1 parent f3bdd1d commit 7fb7bec

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/unique-paths-ii
14+
1315
https://leetcode.cn/problems/rectangle-overlap/
1416

1517
https://leetcode.cn/problems/design-an-expression-tree-with-evaluate-function/

unique-paths-ii/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
2+
if (obstacleGrid[0][0] === 1) return 0;
3+
const m = obstacleGrid.length,
4+
n = obstacleGrid[0].length;
5+
if (obstacleGrid[m - 1][n - 1] === 1) return 0;
6+
7+
const f: number[] = Array(n).fill(0);
8+
f[0] = 1;
9+
for (let i = 0; i < m; i++) {
10+
for (let j = 0; j < n; j++) {
11+
if (obstacleGrid[i][j] === 1) {
12+
f[j] = 0;
13+
} else if (j >= 1 && obstacleGrid[i][j - 1] === 0) {
14+
f[j] = f[j] + f[j - 1];
15+
}
16+
}
17+
}
18+
return f[f.length - 1];
19+
}
20+
export default uniquePathsWithObstacles;

0 commit comments

Comments
 (0)