【Leetcode】【Medium】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 1
and 0
respectively 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.
解题思路:
请参见Unique Paths I 的思路,对于Unique Paths II 我们依然希望使用o(n)的额外空间和o(m+n)的时间复杂度;
Unique Paths II中grid[i][n-1]和grid[i-1][n-1]不再总是相等,即格子中最右侧一列每一格存在的路径条数可能为1或0;
因此,为了继续使用Unique Paths I中数组迭代的技巧,需要每次迭代前计算新一轮的grid[i][n-1]值,这样才能继续计算grid[i][0]~grid[i][n-2]的值,从而完成此次迭代;
如果原始格子内为1,则对应此位值置0,表示从此位置不存在到终点的有效路径。
代码:
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[].size();
vector<int> col (n, ); col[n-] = obstacleGrid[m-][n-] == ? : ;
for (int i = m - ; i >= ; --i) {
col[n-] = obstacleGrid[i][n-] == ? : col[n-];
for (int j = n - ; j >= ; --j) {
col[j] = obstacleGrid[i][j] == ? : col[j] + col[j+];
}
} return col[];
}
};
注:
养成好习惯,除非特殊说明,不要在原始入参上修改,尤其是入参是地址形式。
【Leetcode】【Medium】Unique Paths II的更多相关文章
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 【LeetCode练习题】Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- <LeetCode OJ> 62. / 63. Unique Paths(I / II)
62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- jQuery练习 | 模态对话框(添加删除)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- webpack查缺补漏
webpack是模块化打包工具,通过webpack,可以使得我们更加方便地组织代码.压缩.转译等等. 但是学习webpack也需要一定的成本,这里记录使用webpack许久以来一些模糊的知识点,方便以 ...
- Robot Framework(AutoItLibrary安装)
RobotFramework下安装AutoItLibrary库 1.安装pythonwin32 在下载地址:http://sourceforge.net/projects/pywin32/files/ ...
- selenium+Python(截图保存错误页面)
异常捕捉与错误截图 创建错误截图文件夹,目录结果如下: 用例不可能每一次运行都成功,肯定运行时候有不成功的时候,关键是我们捕捉到错误,并以把并错误截图保存,这将是一个非常棒的功能,也会给我们错误定位带 ...
- 转 shell中$(( )) 与 $( ) 还有${ }的区别
原文 http://blog.zol.com.cn/2322/article_2321763.html $( ) 与 ` ` (反引号)在 bash shell 中,$( ) 与 ` ` (反引号) ...
- Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】
Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- bzoj 2167: 公交车站
Description Z市交通不发达,所有公交路线覆盖的边竟然一个环也不包含,甚至该市的公交路线有可能会分为几个互不连通的块,这可真是不可思议.有一天,你突然听到一条消息,说你的M个同学被困在了Z市 ...
- 开启停止wifi热点bat脚本
@echo offcolor 2title 启停无线WIFI echo 启动WIFI=======>按1键 echo ...
- Ms SQL Server 游标嵌套 初始化数据
--TRUNCATE TABLE TAB_ROLE_FUNC; --SELECT * FROM TAB_ROLE_FUNC; ), ; --外层游标 DECLARE CURSOR_ROLE CURSO ...
- ActiveMQ - 入门指南
首先需要下载ActiveMQ,下面的链接给我们列出了所有版本: http://activemq.apache.org/download-archives.html 每个版本为不同的OS提供了链接: 公 ...