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. 简直喝血!H.265要被专利费活活玩死

    转自 http://news.mydrivers.com/1/440/440145.htm H.264是如今最流行的视频编码格式之一,不但技术先进,而且专利费很低,企业每年只需支付650万美元,而个人 ...

  2. Java JPushV3服务端

    因为JPush的官方文档太乱,所以依据原理自行实现. 主要技术就是post数据到https上和https的auth,实现起来还是很容易的. http://pan.baidu.com/s/1sjEc74 ...

  3. 23----2013.07.01---Div和Span区别,Css常用属性,选择器,使用css的方式,脱离文档流,div+css布局,盒子模型,框架,js基本介绍

    01 复习内容 复习之前的知识点 02演示VS创建元素 03div和span区别 通过display属性进行DIV与Span之间的转换.div->span 设置display:inline   ...

  4. node.js安装方法总结

    为了保持一致,这里也列举三个方法 第一个方法:通过官网下载安装 https://nodejs.org/en/download/ 这种方式的问题是我们需要自己去找网页,找到链接,然后下载 第二个方法:使 ...

  5. chcon可实现对文件的SEAndroid安全标签的修改

    chcon可实现对文件的SEAndroid安全标签的修改 参考使用如下: chcon -u u system/app/ chcon -r object_r system/app/ chcon -t s ...

  6. C# 类构造函数赋值里属性与字段赋值注意项

    public class Test { public Test(int age) { this.Age=age;//如果这里使用的是this.age=age;那么属性里的判断将不会执行 } priva ...

  7. 获取字符串对应的MD5值 (AL16UTF16LE)

    CREATE OR REPLACE FUNCTION fn_md5_utf16le (InputString IN VARCHAR2) RETURN VARCHAR2 IS retval ); /** ...

  8. UIView的frame的扩展分类,轻松取出x、y、height、width等值

    一.引言: 在ios开发中,就界面搭建.控件布局时,都会很恶心的通过很长的代码才能取出控件的x.y.height.width等值,大大降低了开发效率.那为了省略这些恶心的步骤,小编在这里给UIView ...

  9. a标签替代input的submit提交功能

    在工作中有时候会遇到A标签,但是提交表单的时候我们需要用到submit来提交表单,下面几行代码很好的解决了这个问题! <div class="btn"><a hr ...

  10. 4月12日学习笔记——jQuery操作属性和样式

    区分 DOM 属性和元素属性 <img src="images/image.1.jpg" id="hibiscus" alt="Hibiscus ...