Unique Paths II leetcode java
题目:
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]
]
The total number of unique paths is 2.
Note: m and n will be at most 100.
题解:
这道题大体想法跟Unique Path是一样的。
只是要单独考虑下障碍物对整个棋盘的影响。
先看看初始条件会不会受到障碍物的影响。
假设整个棋盘只有一行,那么在第i个位置上设置一个障碍物后,说明位置i到最后一个格子这些路都没法走了。
如果整个棋盘只有一列,那么第i位置上的障碍物,也会影响从第i位置往后的路。
所以说明,在初始条件时,如果一旦遇到障碍物,障碍物后面所有格子的走法都是0。
再看求解过程,当然按照上一题的分析dp[i][j] = dp[i-1][j] + dp[i][j-1] 的递推式依然成立(机器人只能向下或者向右走嘛)。但是,一旦碰到了障碍物,那么这时的到这里的走法应该设为0,因为机器人只能向下走或者向右走,所以到这个点就无法通过。
处理完障碍物的特殊问题,依照unique paths改一下代码就好。
代码如下:
1 public int uniquePathsWithObstacles(int[][] obstacleGrid) {
2 int m = obstacleGrid.length;
3 int n = obstacleGrid[0].length;
4
5 if(m==0||n == 0)
6 return 0;
7
8 if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1)
9 return 0;
int[][] dp = new int[m][n];
dp[0][0] = 1;
for(int i = 1; i < n; i++){
if(obstacleGrid[0][i] == 1)
dp[0][i] = 0;
else
dp[0][i] = dp[0][i-1];
}
for(int i = 1; i < m; i++){
if(obstacleGrid[i][0] == 1)
dp[i][0] = 0;
else
dp[i][0] = dp[i-1][0];
}
for(int i = 1; i < m; i++){
for(int j = 1; j < n; j++){
if(obstacleGrid[i][j] == 1)
dp[i][j] = 0;
else
dp[i][j] = dp[i][j-1] + dp[i-1][j];
}
}
return dp[m-1][n-1];
}
Unique Paths II leetcode java的更多相关文章
- Unique Paths II [LeetCode]
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Unique Paths II ——LeetCode
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
随机推荐
- 领英Linkedin信息搜集工具InSpy
领英Linkedin信息搜集工具InSpy 领英Linkedin是一个知名职业社交媒体网站.通过该网站,渗透测试人员可以获取公司内部组成和员工信息.Kali Linux提供一款专用的信息收集工具I ...
- [USACO06JAN]Redundant Paths
OJ题号:洛谷2860.POJ3177 题目大意: 给定一个无向图,试添加最少的边使得原图中没有桥. 思路: Tarjan缩点,然后统计度为$1$的连通分量的个数(找出原图中所有的桥). 考虑给它们每 ...
- BZOJ1768 : [Ceoi2009]logs
从上到下枚举行,可以$O(m)$更新现在每一列往上连续的1的个数,也可以在$O(m)$的时间内完成排序.总复杂度$O(nm)$. #include<cstdio> #define M 15 ...
- Centos7 安装 ActiveMQ 5.15.1
环境 [root@node1 ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@node1 ~]# uname -r -.el ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem F. Finance 模拟题
Problem F. Finance 题目连接: http://codeforces.com/gym/100714 Description The Big Boss Company (BBC) pri ...
- vs 2010 :类型化数据集DataSet应用
1.启动服务器资源管理器,建立数据库连接 2.在项目中创建数据集 3.为数据集添加表对象 4.为表适配器tableAdapter添加参数化查询 5.修改表适配器的主查询,或添加其他查询 Update: ...
- 简单分享apache封IP的方法
1. 在配置文件里设置: 打开httpd.conf编辑:<Directory “/var/www/html”> Options Indexes FollowSymLinks ...
- 解决Windows x86网易云音乐不能将音乐下载到SD卡的BUG
由于我个人最常用的电脑是Surface pro4 256G版本,装了不少生产力空间还挺吃紧的,音乐之类的必然都存单独的SD卡里.用UWP版本的网易云音乐倒是没问题,最近问题来了,UWP版本的网易云音乐 ...
- Mac下配置Apache服务器
有的时候,我们需要在内网工作组中分享一些文件或是后台接口没有及时给出,你又想要模拟真实数据,直接在项目里创建plist也可以做到这种需求,但难免让工程变得冗余且看起来比较Low.这个时候就看出配置本地 ...
- Microsoft实现的IOC DI之 Unity 、Service Locator、MEF
这几个工具的站点 Microsoft Unity http://unity.codeplex.com Service Locator http://commonservicelocator.code ...