Skip to content

Commit 7495d4f

Browse files
committed
Add solution #63
1 parent 747aaa7 commit 7495d4f

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
60|[Permutation Sequence](./0060-permutation-sequence.js)|Hard|
6969
61|[Rotate List](./0061-rotate-list.js)|Medium|
7070
62|[Unique Paths](./0062-unique-paths.js)|Medium|
71+
63|[Unique Paths II](./0063-unique-paths-ii.js)|Medium|
7172
64|[Minimum Path Sum](./0064-minimum-path-sum.js)|Medium|
7273
65|[Valid Number](./0065-valid-number.js)|Hard|
7374
66|[Plus One](./0066-plus-one.js)|Easy|

solutions/0063-unique-paths-ii.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 63. Unique Paths II
3+
* https://leetcode.com/problems/unique-paths-ii/
4+
* Difficulty: Medium
5+
*
6+
* You are given an m x n integer array grid. There is a robot initially located at
7+
* the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right
8+
* corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at
9+
* any point in time.
10+
*
11+
* An obstacle and space are marked as 1 or 0 respectively in grid. A path that the
12+
* robot takes cannot include any square that is an obstacle.
13+
*
14+
* Return the number of possible unique paths that the robot can take to reach the
15+
* bottom-right corner.
16+
*
17+
* The testcases are generated so that the answer will be less than or equal to 2 * 109.
18+
*/
19+
20+
/**
21+
* @param {number[][]} grid
22+
* @return {number}
23+
*/
24+
var uniquePathsWithObstacles = function(grid) {
25+
const cache = new Array(grid.length).fill(0).map(v => new Array(grid[0].length).fill(0));
26+
27+
function traverse(x, y) {
28+
if (grid[x] === undefined || grid[x][y] === undefined || grid[x][y] === 1) return 0;
29+
if (x === grid.length - 1 && y === grid[0].length - 1) return 1;
30+
if (!cache[x][y]) {
31+
cache[x][y] = traverse(x + 1, y) + traverse(x, y + 1);
32+
}
33+
return cache[x][y];
34+
}
35+
36+
return traverse(0, 0);
37+
};

0 commit comments

Comments
 (0)