[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 ...
随机推荐
- Android基础------高级ul:消息对话框
前言:Android消息对话框提示笔记,刚刚接触Android 1.经典模式 //列表对话框 //经典模式 public void listdialog_01(View view){ final St ...
- Axure RP 的安装与卸载
官网:http://www.axure.com/download 支持Windows和Mac
- 【bzoj1717】[Usaco2006 Dec]Milk Patterns 产奶的模式 后缀数组+离散化
题目描述 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个“模式”. John的牛奶按质量可以被赋予一 ...
- 【bzoj1231】[Usaco2008 Nov]mixup2 混乱的奶牛 状态压缩dp
题目描述 混乱的奶牛[Don Piele, 2007]Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S_i <= ...
- shell脚本学习—条件测试和循环语句
条件测试 1. 条件测试:test [ 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假, 则命令的Exit Status为1(注意与 ...
- queue队列
1.作用:解耦,提高效率.队列就是一个容器,一个有顺序的容器. q.queue.Queue(maxsize=3): 生成一个队列的实例,并且最多存储3个元素 q.get(item,block=Ture ...
- Retrofit工具类
package com.example.week2.retrofitUtils; import android.util.Log; import com.example.week2.model.Con ...
- 如何提升集群资源利用率? 阿里容器调度系统Sigma 深入解析
阿里妹导读:为了保证系统的在线交易服务顺利运转,最初几年,阿里都是在双11大促来临之前大量采购机器储备计算资源,导致了双11之后资源大量闲置点现象.是否能把计算任务与在线服务进行混合部署,在现有弹性资 ...
- UVA.679 Dropping Balls (二叉树 思维题)
UVA.679 Dropping Balls (二叉树 思维题) 题意分析 给出深度为D的完全二叉树,按照以下规则,求第I个小球下落在那个叶子节点. 1. 默认所有节点的开关均处于关闭状态. 2. 若 ...
- ContestHunter暑假欢乐赛 SRM 03
你们也没人提醒我有atcoderQAQ... A题曼哈顿距离=欧拉距离就是在同一行或者同一列,记录下i,j出现过的次数,减去就行,直接map过. B题一开始拿衣服了,一直以为排序和不排序答案是一个样的 ...