[Leetcode刷题总结系列][Dynamic Programming]63. Unique Paths II
2016-05-20 15:28
525 查看
Unique Paths II
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
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.
类似于Unique path,这道题也用动态规划解决。那么对于某一点的路径数量,仍然符合公式:
path[i][j] = path[i-1][j] + path[i][j-1]。
与之前不同的是,此次需要考虑遇到障碍物的情况。显而易见,障碍物处对应的路径数量应该为0。同时,还要考虑到边界情况。比如
[[0]]和
[[1]]。并且对于第一行和第一列的点,障碍物之后和障碍物之下的路径数量都应该为0。
这道题同样可以只用
O(n)空间解决,由于我们从左向右,从上向下计算,所以该数组应当从左向右被新值覆盖,既:
path[i] += path[i-1]
由此可以得到以下代码:
该段代码的运行时间为
O(m*n),空间复杂度为
O(n)
public class Solution { public int uniquePathsWithObstacles(int[][] obstacleGrid) { /* Dynamic Programming Every point in the grid has two approaches to reach, either from above or from left point. So a way to reach a given point is numofpath[i][j] = numofpath[i][j-1] + numofpath[i-1][j] However, consider that there is obstacles in this grid, we should let the numofpath of those points whose value is 1(obstacle) to be 0 since they cannot be passed. At the same time, since we don't need to reconstruct the path, we only need to keep track of the current row we are calculating. And based on the function above, we have to calculat from left to right and replace old values gradually. Then the last one of the array, after the whole loop ends, is what we need. O(m*n) time and O(n) space. */ if (obstacleGrid == null || obstacleGrid.length == 0){ return 0; } int l = obstacleGrid[0].length; //the number of grids in one row int h = obstacleGrid.length; // the number of rows. int[] numofpath = new int[l]; //initialize the array. if there is only one row in the grid, then all points on the right side of the obstacle have 0 path to reach. //if there is only one point in the grid, we have to consider if it is a obstacle or not. numofpath[0] = obstacleGrid[0][0] == 1? 0 : 1; for (int i = 0; i < h; i++){ // the first column of points can only have paths if this point is not an obstacle and its above rows have paths. numofpath[0] = (obstacleGrid[i][0] == 0) && (numofpath[0] != 0)? 1 : 0; for (int j = 1; j < l; j++){ if (obstacleGrid[i][j] == 0){ //there is no obstacle on this grid numofpath[j] += numofpath[j-1]; } if(obstacleGrid[i][j] == 1){ //there is an obstacle on this grid numofpath[j] = 0; } } } return numofpath[l-1]; } }
相关文章推荐
- hdoj-3530-Subsequence
- CRM开发要点(四)
- 26.UIWindow
- runOnUiThread在子进程中更新主进程UI
- kxbdSuperMarquee.js滚动的神器-推荐
- EasyUI tree-grid 数回显问题实现探讨
- Qt之统一的UI界面格式基调,漂亮的UI界面
- 高并发集中访问造成连接MYSQL报Cannot assign requested address错误
- iOS学习之—— UICollectionView导致的崩溃问题
- Build path ->No action available/classpath .project 作用
- [疯狂Java]基础类库:StringBuilder/StringBuffer
- UItableview的一些总结
- iOS学习之—— UIGestureRecognizerState的简单了解
- iOS学习之——改变UITabBarController中当前显示的viewController
- Flex builder3 调试弹出窗口Flex builder cannot locate the required version of Flash Player解决办法
- UI
- Android Studio在连接手机时候,出现"Instant Run requires 'Tools..."不能安装软件的问题
- Java--获取request中所有参数的方法
- iOS学习之—— UIVisualEffectView
- mybatis 数据库表中外键关系存在时 useGeneratedKeys="true" keyProperty="id"