leetcode-63. Unique Paths II · DP + vector
题面
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
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).
Now consider if some obstacles are added to the grids. How many unique paths would there be?

类似我们做过的62. Unique Paths,给定矩阵,其中0-无障碍物, 1-有障碍物(无法通过),找出到达右下角的不重复的路径数。
样例
1. Input:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right 2. Input[[0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],[0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0],[1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,1],[0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0],[0,0,0,1,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,0],[1,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0],[0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,0],[0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0],[0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0],[1,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,1],[0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0],[0,1,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0],[0,1,0,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,1],[1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0],[0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,0,0,1,1],[0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,1],[1,1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1],[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0]]这个样例是做的过程中,遇到的一个棘手样例。int 会爆Output : 1637984640
思路
我们采用和62. Unique Paths一样的思路,到达某点的路径数就是到达它的上边点和左边点路径数的和,只不过要注意的是,当有障碍物时,该点的路径数就要置0(无法到达该点)
算法(直接采用空间压缩DP算法)
时间复杂度:O(m*n)
空间复杂度:O(n)
1. 新建dp[n],预处理第一行(只能往右走,所以出现障碍物之后的点都无法到达置0, 之前的置1)
2. 遍历给定二位矩阵,第一列单独处理(没有左边的点),如果该点为1,即有障碍物,那么该点路径数置0;
3. 其他点正常处理,如果给定矩阵该点为1,即有障碍物,那么dp数组对应点置0;否则,该点路径加和左边点路径。
4. 返回dp末尾元素置。
源码
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int row = obstacleGrid.size(), col = obstacleGrid[].size();
vector<unsigned int> dp(col, );//采用unsigned int 应对样例2的数据
for(int i=; i<col; i++)//第一行预处理
{
if(obstacleGrid[][i] == )
break;
dp[i] = ;
}
for(int i=; i<row; i++)
{
if(obstacleGrid[i][] == )//第一列特殊处理
dp[] = ;
for(int j=; j<col; j++)//处理其他元素
{
if(obstacleGrid[i][j] == )//遇到障碍物,无法通过置0
dp[j] = ;
else
dp[j] += dp[j-];//可以通过,加和左边点路径。
}
}
return dp[col-];
}
};
注意
这里用了一维DP数组来做,用二维DP也可以,结果差不多。
二维DP源码
和一维大同小异
时间复杂度:O(m*n)
空间复杂度:O(m*n)
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int row = obstacleGrid.size(), col = obstacleGrid[].size();
vector<vector<unsigned int>> dp(row, vector<unsigned int>(col, ));//二维vector你get了吗?
for(int i=; i<col; i++)
{
if(obstacleGrid[][i] == )
break;
dp[][i] = ;
}
for(int i=; i<row; i++)
{
if(obstacleGrid[i][] == )
break;
dp[i][] = ;
}
for(int i=; i<row; i++)
{
for(int j=; j<col; j++)
{
if(obstacleGrid[i][j] == )
dp[i][j] = ;
else
dp[i][j] = dp[i][j-] + dp[i-][j];
}
}
return dp[row-][col-];
}
};
leetcode-63. Unique Paths II · DP + vector的更多相关文章
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] 63. Unique Paths II 不同的路径之二
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [leetcode] 63. Unique Paths II (medium)
原题 思路: 用到dp的思想,到row,col点路径数量 : path[row][col]=path[row][col-1]+path[row-1][col]; 遍历row*col,如果map[row ...
- LeetCode: 63. Unique Paths II(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
随机推荐
- CentOS7下搭建zabbix监控(一)——Zabbix监控端配置
zabbix 是一个基于 WEB 界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.zabbix 能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位 ...
- 123457---脑筋急转弯01--com.threeObj03.JiZhuanWan
脑筋急转弯01--com.threeObj03.JiZhuanWan
- ElasticSearch——冷热(hot&warm)架构部署
背景 最近在做订单数据存储到ElasticSearch,考虑到数据量比较大,采用冷热架构来存储,每月建立一个新索引,数据先写入到热索引,通过工具将3个月后的索引自动迁移到冷节点上. ElasticSe ...
- (四)java对象的结构和对象的访问定位
在HotSpot虚拟机中,对象在内存中存储的布局可以分为3块区域:对象头(Header).实例数据(Instance Data)和对齐填充(Padding). 一. 对象头 HotSpot虚拟机的对象 ...
- PostgreSQL学习笔记——内置函数
算术函数(数值计算) +(加).-(减).*(乘)./(除) ABS函数--绝对值: ABS(数值) MOD--求余: MOD(被除数,除数) ROUND--四舍五入: ROUND(对象数值,保留小数 ...
- 第一次搭建redis集群
#端口port 8003 #绑定IPbind 9.1.186.60 #redis 后台运行daemonize yes #开启集群cluster-enabled yes #指定集群配置文件nodes.c ...
- 【c# 学习笔记】为什么要使用委托
上一章中我们可能会很疑惑,为什么需要委托?为什么不直接在MyMethod方法里直接调用Add方法,反而要实例化一个委托对象来完成调用呢?这岂不是自找麻烦吗? 当然,c#引入委托并不是自找麻烦.委托是c ...
- 【持续集成】jenkins安装部署从git获取代码
一:持续集成的概念: 1.1:总体的概括 持续集成Continuous Integration 持续交付Continuous Delivery 持续部署Continuous Deployment 1. ...
- 迷惑性很强的crontab
提到定时任务,我们通常会想起linux的crontab,可以说服务器端大部分定时任务都是由它完成的.这东西固然耗用,但是坑也不少. 这不,昨天我在部署一个备份任务的时候,就不幸踩坑了.差点酿成 ...
- ConcurrentHashMap能完全替代HashTable吗?
至此你应该能够明白,ConcurrentHashMap与HashTable都可以用于多线程的环境,但是当Hashtable的大小增加到一定的时候,性能会急剧下降,因为迭代时需要被锁定很长的时间.因为C ...