63. Unique Paths II
2016-02-18 03:08
435 查看
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as
in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
The total number of unique paths is
Note: m and n will be at most 100.
Solution1 DP Space(m * n)
1ms 19.48%
public class Solution {
public int uniquePathsWithObstacles(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
int m = grid.length;
int n = grid[0].length;
int[][] path = new int[m]
;
for (int i = 0; i < m; i++) {
if (grid[i][0] != 1) {
path[i][0] = 1;
} else {
break;
}
}
for (int i = 0; i < n; i++) {
if (grid[0][i] != 1) {
path[0][i] = 1;
} else {
break;
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (grid[i][j] == 1) {
path[i][j] = 0;
}else {
path[i][j] = path[i - 1][j] + path[i][j - 1];
}
}
}
return path[m - 1][n - 1];
}
}
Solution 2 DP O(n) space
1ms 19.48%
public static int uniquePathsWithObstacles2(int[][] obstacleGrid) {
int width = obstacleGrid[0].length;
int[] dp = new int[width];
dp[0] = 1;
for (int[] row : obstacleGrid) {
for (int j = 0; j < width; j++) {
if (row[j] == 1)
dp[j] = 0;
else if (j > 0)
dp[j] += dp[j - 1];// don't miss +
}
}
return dp[width - 1];
}Solution 3 DP O(1) space
public static int uniquePathsWithObstacles3(int[][] obstacleGrid) {
if(obstacleGrid.length == 0) return 0;
int rows = obstacleGrid.length;
int cols = obstacleGrid[0].length;
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(obstacleGrid[i][j] == 1)
obstacleGrid[i][j] = 0;
else if(i == 0 && j == 0)
obstacleGrid[i][j] = 1;
else if(i == 0)
obstacleGrid[i][j] = obstacleGrid[i][j - 1];// For row 0, if there are no paths to left cell, then its 0,else 1
else if(j == 0)
obstacleGrid[i][j] = obstacleGrid[i - 1][j];// For col 0, if there are no paths to upper cell, then its 0,else 1
else
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
}
}
return obstacleGrid[rows - 1][cols - 1];
}
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as
1and
0respectively
in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[ [0,0,0], [0,1,0], [0,0,0] ]
The total number of unique paths is
2.
Note: m and n will be at most 100.
Solution1 DP Space(m * n)
1ms 19.48%
public class Solution {
public int uniquePathsWithObstacles(int[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
int m = grid.length;
int n = grid[0].length;
int[][] path = new int[m]
;
for (int i = 0; i < m; i++) {
if (grid[i][0] != 1) {
path[i][0] = 1;
} else {
break;
}
}
for (int i = 0; i < n; i++) {
if (grid[0][i] != 1) {
path[0][i] = 1;
} else {
break;
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (grid[i][j] == 1) {
path[i][j] = 0;
}else {
path[i][j] = path[i - 1][j] + path[i][j - 1];
}
}
}
return path[m - 1][n - 1];
}
}
Solution 2 DP O(n) space
1ms 19.48%
public static int uniquePathsWithObstacles2(int[][] obstacleGrid) {
int width = obstacleGrid[0].length;
int[] dp = new int[width];
dp[0] = 1;
for (int[] row : obstacleGrid) {
for (int j = 0; j < width; j++) {
if (row[j] == 1)
dp[j] = 0;
else if (j > 0)
dp[j] += dp[j - 1];// don't miss +
}
}
return dp[width - 1];
}Solution 3 DP O(1) space
public static int uniquePathsWithObstacles3(int[][] obstacleGrid) {
if(obstacleGrid.length == 0) return 0;
int rows = obstacleGrid.length;
int cols = obstacleGrid[0].length;
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
if(obstacleGrid[i][j] == 1)
obstacleGrid[i][j] = 0;
else if(i == 0 && j == 0)
obstacleGrid[i][j] = 1;
else if(i == 0)
obstacleGrid[i][j] = obstacleGrid[i][j - 1];// For row 0, if there are no paths to left cell, then its 0,else 1
else if(j == 0)
obstacleGrid[i][j] = obstacleGrid[i - 1][j];// For col 0, if there are no paths to upper cell, then its 0,else 1
else
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1];
}
}
return obstacleGrid[rows - 1][cols - 1];
}
相关文章推荐
- 62. Unique Paths
- UINavigation的外观设置和页面之间的跳转
- Android酷炫实用的开源框架(UI框架)
- UIScrollView与分页的联合使用
- 1085. Perfect Sequence (25)
- iOS开发之UISearchBar初探
- 截取UIImage指定大小区域
- execute、executeQuery和executeUpdate之间的区别
- The requested URL *** was not found on this server
- 多个UIImage合并成一个UIImage
- 五种创建UIImage的类方法
- iOS开发之UIImage等比缩放
- UIViewController的edgesForExtendedLayout属性
- 自定义导航按钮UIBarButtonItem
- Handler、Loop、MessageQueue的工作原理
- EasyUI系列学习(三)-Draggable(拖动)
- UESTC 1186 Gray code - 简单数位dp
- NSValue包装类
- 读取和解析XML数据----读取XML DocumentBuilderFactory
- 如何获取UIStoryboard里编辑的界面,如何pop回navigationController的某层界面