[Leetcode Week12]Unique Paths II
Unique Paths II 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/unique-paths-ii/description/
Description
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]
]
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
Solution
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int row = obstacleGrid.size();
int col = obstacleGrid[0].size();
int i, j;
vector<int> dp(col, 0);
dp[0] = 1;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (obstacleGrid[i][j] == 1)
dp[j] = 0;
else if (j > 0)
dp[j] += dp[j - 1];
}
}
return dp[col - 1];
}
};
解题描述
这道题是第一道题目的升级版,也真正意义上需要使用动态规划来求解。这道题目中动态规划的最小问题是,到达每一个格子的路径数目等于其上方格子的路径数目加上其左侧格子的路径数目,因为robot每次只能像右走或者向下走。所以整道题目看下来还是从上到下从左到右扫描矩阵,如果某个格子有障碍物,这个格子的路径数置为0,这样其正右方、正下方和右下方的所有格子就都不会加上到达这个有障碍的格子的路径数,从而实现去除不能实现的路径数。
[Leetcode Week12]Unique Paths II的更多相关文章
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
- 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 Week12]Unique Paths
Unique Paths 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths/description/ Description A ...
- [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 ...
- Java for LeetCode 063 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【题解】【矩阵】【回溯】【Leetcode】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 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- [LeetCode][Java] Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
随机推荐
- Python35 升级 pip
使用pip安装插件的时候报错: You are using pip version 8.1.1, however version 9.0.1 is available.You should consi ...
- Mybatis笔记二
一对一查询 案例:查询所有订单信息,订单信息中显示下单人信息. 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则 ...
- Atom Editor 插件 atom-less 的使用方法
一.下载安装 atom-less atom-less 项目在这里:https://github.com/schmuli/atom-less 安装方法请参考这篇博文:http://blog.csdn.n ...
- Json-转自菜鸟教程
1. python中为什么用json有什么作用??不是python用json,json是类似xml的一种通用格式,在很多地方都可以用.json相比xml,数据量更小,而且可以很方便的和解释型语言的结构 ...
- springboot2.0 集成elasticsearch,实现检索、分页、排序
springboot整合es的方式: transport方式(7.0弃用,8.0移除) spring-data(完全当做数据库来用,无法全部支持es,内部也是基于transport,包装后使用非常简单 ...
- POJ3525:Most Distant Point from the Sea——题解
http://poj.org/problem?id=3525 题目大意:给一个逆时针序列的多边形点集,求其中可以画的最大半径的圆的半径. —————————————————————— 二分枚举半径长度 ...
- POJ3977:Subset——题解(三分+折半搜索)
http://poj.org/problem?id=3977 题目大意:有一堆数,取出一些数,记他们和的绝对值为w,取的个数为n,求在w最小的情况下,n最小,并输出w,n. ————————————— ...
- POJ1006:Biorhythms——题解
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- UVA.699 The Falling Leaves (二叉树 思维题)
UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便 ...
- Ubuntu 16.04使用python3.x相关
下载pip3 Python 3.x版本使用pip3,它会把你想下载的包放到usr/local/lib/python3.5/dist-packages/下,而非usr/local/lib/python2 ...