题面

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的更多相关文章

  1. 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). ...

  2. [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 ...

  3. leetcode 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  4. [leetcode] 63. Unique Paths II (medium)

    原题 思路: 用到dp的思想,到row,col点路径数量 : path[row][col]=path[row][col-1]+path[row-1][col]; 遍历row*col,如果map[row ...

  5. LeetCode: 63. Unique Paths II(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/

  6. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  7. 【LeetCode】63. Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  8. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  9. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

随机推荐

  1. Yarn概述——FAST, RELIABLE, AND SECURE DEPENDENCY MANAGEMENT

    官网链接:https://yarnpkg.com/lang/en/ 特性 Ultra Fast. Yarn caches every package it downloads so it never ...

  2. unique_ptr智能指针

    一.VS例子 // Test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <thread> #includ ...

  3. 123457123456#0#-----com.twoapp.jingPinYinYu01----儿童学英语jiemei

    com.twoapp.jingPinYinYu01----儿童学英语jiemei

  4. mysql常用命令、非交互式mysql命令看29条

    CentOS下mysql数据库常用命令总结1.更改root密码 mysqladmin -uroot password 'yourpassword' 2.远程登陆mysql服务器 mysql -uroo ...

  5. Java中将一个反斜杠转换成两个反斜杠

    代码示例: s = s.replaceAll("\\\\", "\\\\\\\\");

  6. iOS技术面试03:Foundation

    是否可以把比较耗时的操作放在NSNotificationCenter中 如果在异步线程发的通知,那么可以执行比较耗时的操作: 如果在主线程发的通知,那么就不可以执行比较耗时的操作 3.Foundati ...

  7. C/C++查漏补缺(常更)

    一.#define宏定义 如下程序段,则输出结果为: #define MAX 12 int main(){ cout << "20\0MAX019" << ...

  8. 使用说明(2S)

    [Build Status] 功能 系统代理设置 PAC 模式和全局模式 [GFWList] 和用户规则 支持 HTTP 代理 支持多服务器切换 支持 UDP 代理 支持插件 下载 下载 [最新版]. ...

  9. 三节课MINI计划第五周

    一.任务及干货 二.作品 (一)小组分工 (二)社群运营方案

  10. DSP6455的EMIFA口

    DSP6455的EMIFA口 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 支持两种不同的接口模式: 异步接口:标准的SRAM,ROM接口 同步接口:SBS ...