|
1 | 1 | # [63. Unique Paths II](https://leetcode.com/problems/unique-paths-ii)
|
2 | 2 |
|
3 | 3 | ## Description
|
4 |
| -<p>A robot is located at the top-left corner of a <em>m</em> x <em>n</em> grid (marked 'Start' in the diagram below).</p> |
5 |
| -
|
6 |
| -<p>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).</p> |
7 |
| -
|
8 |
| -<p>Now consider if some obstacles are added to the grids. How many unique paths would there be?</p> |
9 |
| -
|
10 |
| -<p><img src="https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png" style="width: 400px; height: 183px;" /></p> |
11 |
| -
|
12 |
| -<p>An obstacle and empty space is marked as <code>1</code> and <code>0</code> respectively in the grid.</p> |
13 |
| -
|
14 |
| -<p><strong>Note:</strong> <em>m</em> and <em>n</em> will be at most 100.</p> |
15 |
| -
|
16 |
| -<p><strong>Example 1:</strong></p> |
17 |
| -
|
18 |
| -<pre> |
19 |
| -<strong>Input: |
20 |
| -</strong>[ |
21 |
| - [0,0,0], |
22 |
| - [0,1,0], |
23 |
| - [0,0,0] |
24 |
| -] |
25 |
| -<strong>Output:</strong> 2 |
26 |
| -<strong>Explanation:</strong> |
27 |
| -There is one obstacle in the middle of the 3x3 grid above. |
28 |
| -There are two ways to reach the bottom-right corner: |
29 |
| -1. Right -> Right -> Down -> Down |
30 |
| -2. Down -> Down -> Right -> Right |
31 |
| -</pre> |
| 4 | +<p>A robot is located at the top-left corner of a <em>m</em> x <em>n</em> grid (marked 'Start' in the diagram below).</p> |
32 | 5 |
|
33 | 6 |
|
34 | 7 |
|
35 |
| -## Solutions |
| 8 | +<p>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).</p> |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +<p>Now consider if some obstacles are added to the grids. How many unique paths would there be?</p> |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +<p><img src="https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png" style="width: 400px; height: 183px;" /></p> |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +<p>An obstacle and empty space is marked as <code>1</code> and <code>0</code> respectively in the grid.</p> |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +<p><strong>Note:</strong> <em>m</em> and <em>n</em> will be at most 100.</p> |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +<p><strong>Example 1:</strong></p> |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +<pre> |
36 | 33 |
|
| 34 | +<strong>Input: |
37 | 35 |
|
| 36 | +</strong>[ |
| 37 | + |
| 38 | + [0,0,0], |
| 39 | + |
| 40 | + [0,1,0], |
| 41 | + |
| 42 | + [0,0,0] |
| 43 | + |
| 44 | +] |
| 45 | + |
| 46 | +<strong>Output:</strong> 2 |
| 47 | + |
| 48 | +<strong>Explanation:</strong> |
| 49 | + |
| 50 | +There is one obstacle in the middle of the 3x3 grid above. |
| 51 | + |
| 52 | +There are two ways to reach the bottom-right corner: |
| 53 | + |
| 54 | +1. Right -> Right -> Down -> Down |
| 55 | + |
| 56 | +2. Down -> Down -> Right -> Right |
| 57 | + |
| 58 | +</pre> |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +## Solutions |
| 64 | + |
| 65 | +### Go |
| 66 | +```go |
| 67 | +func uniquePathsWithObstacles(obstacleGrid [][]int) int { |
| 68 | + m,n := len(obstacleGrid),len(obstacleGrid[0]) |
| 69 | + dp := make([][]int,m) |
| 70 | + for i:=0; i < m;i++ { |
| 71 | + dp[i] = make([]int,n) |
| 72 | + } |
| 73 | + for i := 0; i < m; i++ { |
| 74 | + for j := 0; j < n; j++ { |
| 75 | + if obstacleGrid[i][j] == 0 { |
| 76 | + if i == 0 && j == 0 { |
| 77 | + dp[i][j] = 1 |
| 78 | + } else if i > 0 && j >0 { |
| 79 | + dp[i][j] = dp[i][j-1]+dp[i-1][j] |
| 80 | + } else if i > 0 { |
| 81 | + dp[i][j] = dp[i-1][j] |
| 82 | + } else { |
| 83 | + dp[i][j] = dp[i][j-1] |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return dp[m-1][n-1] |
| 89 | +} |
| 90 | +``` |
38 | 91 | ### Python3
|
39 | 92 |
|
40 | 93 | ```python
|
|
0 commit comments