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.

Note

m and n will be at most 100.

Example

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.

Analysis:

DP: d[i][j] = d[i][j-1]+d[i-1][j].

NOTE: We can use 1D array to perform the DP. Since d[i][j] depends on d[i][j-1], i.e., the new d[][j-1], we should increase j from 0 to end. If d[i][j] depends on d[i-1][j-1] then we should decrease j from end to 0.

Solution:

 public class Solution {
/**
* @param obstacleGrid: A list of lists of integers
* @return: An integer
*/
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int rowNum = obstacleGrid.length;
if (rowNum==0) return 0;
int colNum = obstacleGrid[0].length;
if (colNum==0) return 0;
if (obstacleGrid[0][0]==1) return 0; int[] path = new int[colNum];
path[0] =1;
for (int i=1;i<colNum;i++)
if (obstacleGrid[0][i]==1) path[i]=0;
else path[i] = path[i-1]; for (int i=1;i<rowNum;i++){
if (obstacleGrid[i][0]==1) path[0]=0;
for (int j=1;j<colNum;j++)
if (obstacleGrid[i][j]==1) path[j]=0;
else path[j]=path[j-1]+path[j]; } return path[colNum-1];
}
}

LintCode-Unique Path II的更多相关文章

  1. LeetCode 63. Unique Path II(所有不同路径之二)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  2. leetcode63—Unique Path II

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  3. Unique path ii

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

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

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

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

  6. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

  7. 【LeetCode】63. Unique Paths II

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

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

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

  9. 62. Unique Paths && 63 Unique Paths II

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

  10. 【leetcode】Unique Paths II

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

随机推荐

  1. SharePoint 2010 "客户端不支持使用windows资源管理器打开此列表" 解决方法

    SharePoint 2010 在“库”--“库工具”,有一个“使用资源管理器打开”的按钮,点上去报“客户端不支持使用windows资源管理器打开此列表”.如图: 解决方案:在“开始”--“管理工具” ...

  2. Set集合——HashSet、TreeSet、LinkedHashSet(2015年07月06日)

    一.Set集合不同于List的是: Set不允许重复 Set是无序集合 Set没有下标索引,所以对Set的遍历要通过迭代器Iterator 二.HashSet 1.HashSet由一个哈希表支持,内部 ...

  3. css3 transfrom使用以及其martix(矩阵)属性与其它属性的关系

    写法 其属性martix与skew .scale .translate之间的关系   兼容性 :       IE9+ : -ms-transform :  IE9只支持2D转换       fire ...

  4. AngularJS 学习笔记(1)

    AngularJS是一款前端JS框架.AngularJS官网 http://angularjs.org [开发环境准备]: 1,下载AngularJS:JS and CSS in Solution 2 ...

  5. DIV+CSS解决IE6,IE7,IE8,FF兼容问题

    1.IE8下兼容问题,这个最好处理,转化成IE7兼容就可以.在头部加如下一段代码,然后只要在IE7下兼容了,IE8下面也就兼容了:1. <metahttp-equivmetahttp-equiv ...

  6. Android使用JNI(从java调用本地函数)

    当编写一个混合有本地C代码和Java的应用程序时,需要使用Java本地接口(JNI)作为连接桥梁.JNI作为一个软件层和API,允许使用本地代码调用Java对象的方法,同时也允许在Java方法中调用本 ...

  7. VpnService

    这段时间项目中使用到了VpnService,整理了一下官方文档的资料 VpnService is a base class for applications to extend and build t ...

  8. Warning: Permanently added '...' (RSA) to the list of known hosts --Windows下git bash 警告处理

    原链接地址 StackOverflow 处理方法: 创建文件~/.ssh/config, 此处对应windows当前用户目录下的.ssh文件夹 增加如下语句 UserKnownHostsFile ~/ ...

  9. jquery 中如何将数组转化为json字符串,然后再转化回来?

    其实可以这样: $.fn.stringify = function() { return JSON.stringify(this); } 然后这样调用: $(array).stringify(); 转 ...

  10. 工作单元(Unit of Work)

    维护受业务事务影响的对象列表,并协调变化的写入和并发问题的解决. 从DB中存取Data时,必须记录增删改动作,以将对DB有影响的数据写会到DB中去. 如果在每次修改对象模型时就对DB进行相应的修改,会 ...