LintCode-Unique Path II
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.
m and n will be at most 100.
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的更多相关文章
- LeetCode 63. Unique Path II(所有不同路径之二)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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 ...
- Unique path ii
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- [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】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- 【leetcode】Unique Paths II
Unique Paths II Total Accepted: 22828 Total Submissions: 81414My Submissions Follow up for "Uni ...
随机推荐
- CSS图片翻转例子
dfdfdfdfdf <!DOCTYPE html> <html> <head> <meta charset=" ...
- 【CSS3】---@font-face
- 《深入浅出WPF》 学习笔记
<深入浅出WPF> 序言 1. 什么是WPF 2. 为什么要学习WPF 第一章 XAML概览 1. XAML是什么? 2. XAML有哪些优点 第二章 从零起步认识XAML 1. 新 ...
- iOS - 表格
一. TableView 1.1 StoryBoard方式 1.2 nib方式 1.2.1 一般 1.2.2 自定义单元格 1.3 纯代码方式 (1) 简单表视图操作 Step1: 实现协议 2个协议 ...
- ACCESS的System.Data.OleDb.OleDbException: INSERT INTO 语句的语法错误
一直用的是SQL 数据库,突然改用Access了,使用起来就是没有SQL 顺畅,老是出来些意想不到的错误.今天用Access做的网站程序进行添加数据,调试了一下午,总是异常…… 提示ACCESS的Sy ...
- 判断Featureclass的类型
一个Featureclass可以是Shapefile Feature Class.Personal Geodatabase Feature Class.File Geodatabase Feature ...
- Windows 命令大全
打开控制面板的方法:输入control,回车即可打开. 以下是“运行”里常见的命令: gpedit.msc-----组策略 sndrec32-------录音机 Nslookup-------IP地址 ...
- uml的关联多重度
UML中关联的多重度是指一个类的实例能够与另一个类的多少个实例相关联,这个“多少”被称为关联角色的多重度指定关联一端的多重度.也可以这样理解:在关联另一端的类的每个对象要求在本端的类必须有多 少个对象 ...
- HDU 1954 Subway tree systems (树的最小表示法)
题意:用一个字符串表示树,0代表向下走,1代表往回走,求两棵树是否同构. 分析:同构的树经过最小表示会转化成两个相等的串. 方法:递归寻找每一棵子树,将根节点相同的子树的字符串按字典序排列,递归回去即 ...
- Regionals 2013 :: North America - Southeast USA
Regionals 2013 :: North America - Southeast USA It Takes a Village As a Sociologist, you are studyin ...