Question

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.

Solution

Similar with "Unique Paths", there are two differences to be considered:

1. for dp[0][i] and dp[i][0], if there exists previous element which equals to 1, then the rest elements are all unreachable.

2. for dp[i][j], if it equals to 1, then it's unreachable.

 public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
if (obstacleGrid == null)
return 0;
int m = obstacleGrid.length, n = obstacleGrid[0].length;
int[][] dp = new int[m][n];
// Check first element
if (obstacleGrid[0][0] == 1)
return 0;
else
dp[0][0] = 1;
// Left column
for (int i = 1; i < m; i++) {
if (obstacleGrid[i][0] == 1)
dp[i][0] = 0;
else
dp[i][0] = dp[i - 1][0];
}
// Top row
for (int i = 1; i < n; i++) {
if (obstacleGrid[0][i] == 1)
dp[0][i] = 0;
else
dp[0][i] = dp[0][i - 1];
}
// Inside
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 - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
}

Unique Paths II 解答的更多相关文章

  1. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  2. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  3. 62. Unique Paths && 63 Unique Paths II

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

  4. 【leetcode】Unique Paths II

    Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...

  5. 61. Unique Paths && Unique Paths II

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  6. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  7. 【LeetCode练习题】Unique Paths II

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

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

  9. LEETCODE —— Unique Paths II [Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

随机推荐

  1. log4net logfornet 配置和用法

    较好的参考地址: http://in3040.blog.163.com/blog/static/116702443201091354028744/ http://dev.tot.name/dotnet ...

  2. JAX-WS 可运行项目

    该项目是通过JAX-WS实现的WebService服务,其中包括了1.关于最简单的WebService服务的创建2.关于文件交互的WebService的创建 代码中包括了服务端代码和客户端代码(客户端 ...

  3. 主题简介 ASP .NET

    由控件的外观.样式组成的集合,由一个文件组构成,存放在App_Themes文件夹下. 主题包括:皮肤文件(.Skin).CSS文件(.CSS).图片.其它资源等. 主题的作用:统一设置Web页面的外观 ...

  4. Https协议简析及中间人攻击原理

    1.基础知识 1.1 对称加密算法 对称加密算法的特点是加密密钥和解密密钥是同一把密钥K,且加解密速度快,典型的对称加密算法有DES.AES等                              ...

  5. 数据结构c++语言描述——最大堆(MaxHeap)

    一.最大堆的插入 图9-3a 给出了一个具有5个元素的最大堆.由于堆是完全二叉树,当加入一个元素形成6元素堆时,其结构必如9-3b 所示.如果插入元素的值为1,则插入后该元素成为2的左孩子,相反,若新 ...

  6. WP8.1 页面导航 缓存问题

    最近开始学习wp8.1开发,在页面的导航学习时发现了一点问题,即当使用Frame.Navigate()方法进行页面的跳转时,每次都会重新实例化一个页面.而在新的页面采用Frame.GoBack()或者 ...

  7. Hibernate常见接口说明

    (一)SessionFactory 1. getCurrentSession()和openSession()区别 getCurrentSession创建的session会和绑定到当前线程,而openS ...

  8. C++服务器设计(二):应用层I/O缓冲

    数据完整性讨论 我们已经选择了I/O复用模型作为系统底层I/O模型.但是我们并没有具体解决读写问题,即在我们的Reactor模式中,我们怎么进行读写操作,才能保证对于每个连接的发送或接收的数据是完整的 ...

  9. uva 1597 Searching the Web

    The word "search engine" may not be strange to you. Generally speaking, a search engine se ...

  10. (转)创建和查看Javadoc文档

    原地址:http://jinnaxu-tju-edu-cn.iteye.com/blog/667177 Javadoc是Sun公司提供的一个技术,它从程序源代码中抽取类.方法.成员等注释形成一个和源代 ...