[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 ...
随机推荐
- C#中WVVM的使用
学习WVVM模式,设计一个简单的菜单显示和选择时显示个数的一个例子. 最终效果: 所建文件结构如下: MenuModel:菜品属性-名称和价格 using System; using System.C ...
- 在网页中浏览PDF文档
刚开始找了好多插件,包括pdf.js,但都不理想,后来发现用iframe反而容易: <iframe src="test_pdf.pdf" width="800&qu ...
- BZOJ 1818 内部白点(离散化+树状数组)
此题就是1227 的弱化版. 画个图或者稍微证明一下就能够知道,一定不会超过一次变换. 那么我们只需要统计有多少个白点会变黑,换句话说就是有多少个白点上下左右都有黑点. 离散化横坐标,因为没有黑点在的 ...
- CentOS 安装MariaDB
1.安装 #同时安装mariadb和mariadb-server [root@bigdata-senior01 yum.repos.d]# yum -y install mariadb mariadb ...
- 洛谷 P1969 积木大赛 解题报告
P1969 积木大赛 题目描述 春春幼儿园举办了一年一度的"积木大赛".今年比赛的内容是搭建一座宽度为\(n\)的大厦,大厦可以看成由\(n\)块宽度为1的积木组成,第\(i\)块 ...
- Django Model 数据表
Django Model 定义语法 版本:1.7主要来源:https://docs.djangoproject.com/en/1.7/topics/db/models/ 简单用法 from djang ...
- JavaScript中进制之间的转换
JavaScript中进制之间的转换 //十进制转其他 var x = 100; alert(x); alert(x.toString(2)); //转2进制 alert(x.toString(8)) ...
- 在局域网中基于Windows文件共享的git环境搭建
本文的思想是在局域网中用一台电脑作为服务器,在其中建立一个文件夹,作为总的公开版本库.然后将这个文件夹共享,使其他客户机都可以访问,从而进行代码的管理. 一.下载安装文件 1.git核心: git-f ...
- iOS开发ARC机制下的内存管理技术要点
转载一篇: iOS开发ARC内存管理技术要点.ARC内存管理原则总结.iOS ARC内存管理总结 ARC内存管理机制 (一)ARC的判断准则: 只要没有任何一个强指针指向该对象,该对象就会被释放. ( ...
- HDU 5538 (水不水?)
House Building Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...