File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 68
68
60|[ Permutation Sequence] ( ./0060-permutation-sequence.js ) |Hard|
69
69
61|[ Rotate List] ( ./0061-rotate-list.js ) |Medium|
70
70
62|[ Unique Paths] ( ./0062-unique-paths.js ) |Medium|
71
+ 63|[ Unique Paths II] ( ./0063-unique-paths-ii.js ) |Medium|
71
72
64|[ Minimum Path Sum] ( ./0064-minimum-path-sum.js ) |Medium|
72
73
65|[ Valid Number] ( ./0065-valid-number.js ) |Hard|
73
74
66|[ Plus One] ( ./0066-plus-one.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments