File tree 5 files changed +77
-0
lines changed
5 files changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -279,6 +279,7 @@ All solutions will be accepted!
279
279
| 430| [ Flatten A Multilevel Doubly Linked List] ( https://leetcode-cn.com/problems/flatten-a-multilevel-doubly-linked-list/description/ ) | [ java/py] ( ./algorithms/FlattenAMultilevelDoublyLinkedList ) | Medium|
280
280
| 138| [ Copy List With Random Pointer] ( https://leetcode-cn.com/problems/copy-list-with-random-pointer/description/ ) | [ java/py/js] ( ./algorithms/CopyListWithRandomPointer ) | Medium|
281
281
| 62| [ Unique Paths] ( https://leetcode-cn.com/problems/unique-paths/description/ ) | [ java/py/js] ( ./algorithms/UniquePaths ) | Medium|
282
+ | 63| [ Unique Paths II] ( https://leetcode-cn.com/problems/unique-paths-ii/description/ ) | [ java/py/js] ( ./algorithms/UniquePathsII ) | Medium|
282
283
| 162| [ Find Peak Element] ( https://leetcode-cn.com/problems/find-peak-element/description/ ) | [ java/py/js] ( ./algorithms/FindPeakElement ) | Medium|
283
284
| 200| [ Number Of Islands] ( https://leetcode-cn.com/problems/number-of-islands/description/ ) | [ java/py/js] ( ./algorithms/NumberOfIslands ) | Medium|
284
285
| 223| [ Rectangle Area] ( https://leetcode-cn.com/problems/rectangle-area/description/ ) | [ java/py/js] ( ./algorithms/RectangleArea ) | Medium|
Original file line number Diff line number Diff line change
1
+ # Unique Paths II
2
+ This problem is easy to solve by dynamic programming, like [ Unique Paths] ( ./UniquePaths )
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int uniquePathsWithObstacles (int [][] obstacleGrid ) {
3
+ int m = obstacleGrid .length ;
4
+ int n = m > 0 ? obstacleGrid [0 ].length : 0 ;
5
+
6
+ if (m == 0 || n == 0 )
7
+ return 0 ;
8
+
9
+ int [][] dp = new int [m ][n ];
10
+ dp [0 ][0 ] = obstacleGrid [0 ][0 ] == 0 ? 1 : 0 ;
11
+
12
+ for (int i = 0 ; i < m ; i ++) {
13
+ for (int j = 0 ; j < n ; j ++) {
14
+ if (obstacleGrid [i ][j ] == 0 )
15
+ dp [i ][j ] += (i > 0 ? dp [i - 1 ][j ] : 0 ) + (j > 0 ? dp [i ][j - 1 ] : 0 );
16
+ }
17
+ }
18
+
19
+ return dp [m - 1 ][n - 1 ];
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } obstacleGrid
3
+ * @return {number }
4
+ */
5
+ var uniquePathsWithObstacles = function ( obstacleGrid ) {
6
+ let m = obstacleGrid . length ,
7
+ n = m > 0 ? obstacleGrid [ 0 ] . length : 0
8
+
9
+ if ( m == 0 || n == 0 )
10
+ return 0
11
+
12
+ dp = [ ]
13
+ for ( let i = 0 ; i < m ; i ++ ) {
14
+ let row = new Array ( n )
15
+ row . fill ( 0 )
16
+ dp . push ( row )
17
+ }
18
+
19
+ dp [ 0 ] [ 0 ] = obstacleGrid [ 0 ] [ 0 ] == 0 ? 1 : 0
20
+
21
+ for ( let i = 0 ; i < m ; i ++ ) {
22
+ for ( let j = 0 ; j < n ; j ++ ) {
23
+ if ( obstacleGrid [ i ] [ j ] == 0 )
24
+ dp [ i ] [ j ] += ( i > 0 ? dp [ i - 1 ] [ j ] : 0 ) + ( j > 0 ? dp [ i ] [ j - 1 ] : 0 )
25
+ }
26
+ }
27
+
28
+ return dp [ m - 1 ] [ n - 1 ]
29
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def uniquePathsWithObstacles (self , obstacleGrid ):
3
+ """
4
+ :type obstacleGrid: List[List[int]]
5
+ :rtype: int
6
+ """
7
+ m = len (obstacleGrid )
8
+ n = len (obstacleGrid [0 ]) if m > 0 else 0
9
+ if m == 0 or n == 0 :
10
+ return 0
11
+
12
+ dp = []
13
+ for i in xrange (m ):
14
+ row = [0 ] * n
15
+ dp .append (row )
16
+
17
+ dp [0 ][0 ] = 1 if obstacleGrid [0 ][0 ] == 0 else 0
18
+
19
+ for i in xrange (m ):
20
+ for j in xrange (n ):
21
+ if obstacleGrid [i ][j ] == 0 :
22
+ dp [i ][j ] += (dp [i - 1 ][j ] if i > 0 else 0 ) + (dp [i ][j - 1 ] if j > 0 else 0 )
23
+
24
+ return dp [m - 1 ][n - 1 ]
You can’t perform that action at this time.
0 commit comments