Skip to content

Commit 66c8cd2

Browse files
committed
0063. Unique Paths II
1 parent 75cbd9c commit 66c8cd2

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed

markdown/0063. Unique Paths II.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
### [63\. Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
A robot is located at the top-left corner of a _m_ x _n_ grid (marked 'Start' in the diagram below).
7+
8+
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).
9+
10+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
11+
12+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
13+
14+
An obstacle and empty space is marked as `1` and `0` respectively in the grid.
15+
16+
**Note:** _m_ and _n_ will be at most 100.
17+
18+
**Example 1:**
19+
20+
```
21+
Input:
22+
[
23+
  [0,0,0],
24+
  [0,1,0],
25+
  [0,0,0]
26+
]
27+
Output: 2
28+
Explanation:
29+
There is one obstacle in the middle of the 3x3 grid above.
30+
There are two ways to reach the bottom-right corner:
31+
1\. Right -> Right -> Down -> Down
32+
2\. Down -> Down -> Right -> Right
33+
```
34+
35+
36+
#### My Solution
37+
38+
Language: **Java**
39+
40+
```java
41+
class Solution {
42+
   public int uniquePathsWithObstacles(int[][] obstacleGrid) {
43+
       if (obstacleGrid == null) {
44+
           return 0;
45+
      }
46+
       int m = obstacleGrid.length;
47+
       if (m == 0) {
48+
           return 0;
49+
      }
50+
       int n = obstacleGrid[0].length;
51+
       int[][] ways = new int[m][n]; // 所有的格子都能到达,因此初始化为0,表示没有计算过。该数组用于保存到达该坐标的路径个数,防止重复计算
52+
       int i = uniquePathsWithObstacles(obstacleGrid, ways, m - 1, n - 1);
53+
       return i;
54+
  }
55+
56+
   private int uniquePathsWithObstacles(int[][] obstacleGrid, int[][] ways, int m, int n) {
57+
       if (m < 0 || n < 0 || obstacleGrid[m][n] == 1) {
58+
           return 0;
59+
      }
60+
       if (m + n == 0) {
61+
           ways[m][n] = 1;
62+
           return 1;
63+
      }
64+
       if (ways[m][n] > 0) {
65+
           return ways[m][n];
66+
      }
67+
       ways[m][n] = uniquePathsWithObstacles(obstacleGrid, ways, m, n - 1) + uniquePathsWithObstacles(obstacleGrid, ways, m - 1, n);
68+
       return ways[m][n];
69+
  }
70+
}
71+
```
72+
73+
#### Recommend Solution
74+
75+
Language: **Java**
76+
77+
```java
78+
class Solution {
79+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
80+
int width = obstacleGrid[0].length;
81+
int[] dp = new int[width];
82+
dp[0] = 1;
83+
for (int[] row : obstacleGrid) {
84+
for (int j = 0; j < width; j++) {
85+
if (row[j] == 1)
86+
dp[j] = 0;
87+
else if (j > 0)
88+
dp[j] += dp[j - 1];
89+
}
90+
}
91+
return dp[width - 1];
92+
}
93+
94+
// dp[j] += dp[j - 1];
95+
// is
96+
// dp[j] = dp[j] + dp[j - 1];
97+
98+
// which is new dp[j] = old dp[j] + dp[j-1]
99+
// which is current cell = top cell + left cell
100+
}
101+
```
102+
![](https://image-hosting-1251780645.cos.ap-beijing.myqcloud.com/20190717000607.png)

src/main/java/leetcode/_63_/Main.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode._63_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
int[][] obstacleGrid = {{0, 1, 1}, {1, 1, 1}, {0, 0, 0}};
10+
System.out.println(solution.uniquePathsWithObstacles(obstacleGrid));
11+
int[][] obstacleGridw = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};
12+
System.out.println(solution.uniquePathsWithObstacles(obstacleGridw));
13+
int[][] obstacleGridwq = {{0}};
14+
System.out.println(solution.uniquePathsWithObstacles(obstacleGridwq));
15+
int[][] obstacleGridwq2 = {{1}};
16+
System.out.println(solution.uniquePathsWithObstacles(obstacleGridwq2));
17+
}
18+
}
19+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package leetcode._63_;
2+
3+
class Solution {
4+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
5+
if (obstacleGrid == null) {
6+
return 0;
7+
}
8+
int m = obstacleGrid.length;
9+
if (m == 0) {
10+
return 0;
11+
}
12+
int n = obstacleGrid[0].length;
13+
int[][] ways = new int[m][n]; // 所有的格子都能到达,因此初始化为0,表示没有计算过。该数组用于保存到达该坐标的路径个数,防止重复计算
14+
int i = uniquePathsWithObstacles(obstacleGrid, ways, m - 1, n - 1);
15+
return i;
16+
}
17+
18+
private int uniquePathsWithObstacles(int[][] obstacleGrid, int[][] ways, int m, int n) {
19+
if (m < 0 || n < 0 || obstacleGrid[m][n] == 1) {
20+
return 0;
21+
}
22+
if (m + n == 0) {
23+
ways[m][n] = 1;
24+
return 1;
25+
}
26+
if (ways[m][n] > 0) {
27+
return ways[m][n];
28+
}
29+
ways[m][n] = uniquePathsWithObstacles(obstacleGrid, ways, m, n - 1) + uniquePathsWithObstacles(obstacleGrid, ways, m - 1, n);
30+
return ways[m][n];
31+
}
32+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
### [63\. Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
A robot is located at the top-left corner of a _m_ x _n_ grid (marked 'Start' in the diagram below).
7+
8+
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).
9+
10+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
11+
12+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
13+
14+
An obstacle and empty space is marked as `1` and `0` respectively in the grid.
15+
16+
**Note:** _m_ and _n_ will be at most 100.
17+
18+
**Example 1:**
19+
20+
```
21+
Input:
22+
[
23+
  [0,0,0],
24+
  [0,1,0],
25+
  [0,0,0]
26+
]
27+
Output: 2
28+
Explanation:
29+
There is one obstacle in the middle of the 3x3 grid above.
30+
There are two ways to reach the bottom-right corner:
31+
1\. Right -> Right -> Down -> Down
32+
2\. Down -> Down -> Right -> Right
33+
```
34+
35+
36+
#### My Solution
37+
38+
Language: **Java**
39+
40+
```java
41+
class Solution {
42+
   public int uniquePathsWithObstacles(int[][] obstacleGrid) {
43+
       if (obstacleGrid == null) {
44+
           return 0;
45+
      }
46+
       int m = obstacleGrid.length;
47+
       if (m == 0) {
48+
           return 0;
49+
      }
50+
       int n = obstacleGrid[0].length;
51+
       int[][] ways = new int[m][n]; // 所有的格子都能到达,因此初始化为0,表示没有计算过。该数组用于保存到达该坐标的路径个数,防止重复计算
52+
       int i = uniquePathsWithObstacles(obstacleGrid, ways, m - 1, n - 1);
53+
       return i;
54+
  }
55+
56+
   private int uniquePathsWithObstacles(int[][] obstacleGrid, int[][] ways, int m, int n) {
57+
       if (m < 0 || n < 0 || obstacleGrid[m][n] == 1) {
58+
           return 0;
59+
      }
60+
       if (m + n == 0) {
61+
           ways[m][n] = 1;
62+
           return 1;
63+
      }
64+
       if (ways[m][n] > 0) {
65+
           return ways[m][n];
66+
      }
67+
       ways[m][n] = uniquePathsWithObstacles(obstacleGrid, ways, m, n - 1) + uniquePathsWithObstacles(obstacleGrid, ways, m - 1, n);
68+
       return ways[m][n];
69+
  }
70+
}
71+
```
72+
73+
#### Recommend Solution
74+
75+
Language: **Java**
76+
77+
```java
78+
class Solution {
79+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
80+
int width = obstacleGrid[0].length;
81+
int[] dp = new int[width];
82+
dp[0] = 1;
83+
for (int[] row : obstacleGrid) {
84+
for (int j = 0; j < width; j++) {
85+
if (row[j] == 1)
86+
dp[j] = 0;
87+
else if (j > 0)
88+
dp[j] += dp[j - 1];
89+
}
90+
}
91+
return dp[width - 1];
92+
}
93+
94+
// dp[j] += dp[j - 1];
95+
// is
96+
// dp[j] = dp[j] + dp[j - 1];
97+
98+
// which is new dp[j] = old dp[j] + dp[j-1]
99+
// which is current cell = top cell + left cell
100+
}
101+
```
102+
![](https://image-hosting-1251780645.cos.ap-beijing.myqcloud.com/20190717000607.png)

0 commit comments

Comments
 (0)