唯一路径问题II

Unique Paths 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.

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.

--

第一种方法(uniquePathsWithObstacles)为递归实现

  会超时,最后一个case有16亿+条路径...递归方法会走每条路径,所以一定会超时。

第二种方法(uniquePathsWithObstaclesDP)为动态规划

  不难发现max_ways[x,y]=max_ways[x-1,y]+max_ways[x,y-1], 即满足最优子结构性质。

  并且max_ways[x-1,y]和max_ways[x,y-1]依赖于max_ways[m,n](0<m<x, 0<n<y),即满足子问题重叠性质,因此使用动态规划可以获得更好的效率

 
 
'''
Created on Nov 25, 2014 @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
'''
class Solution:
def __init__(self):
self.ways=0
self.max_x=0
self.max_y=0 # @param obstacleGrid, a list of lists of integers
# @return an integer
def uniquePathsWithObstacles(self, obstacleGrid):
if(obstacleGrid==None):return 0
if(len(obstacleGrid)==0):return 0
if(obstacleGrid[0][0] ==1): return 0 self.__init__()
self.max_x=len(obstacleGrid[0])-1
self.max_y=len(obstacleGrid)-1
self.find_ways(0,0, obstacleGrid)
return self.ways def find_ways(self, x, y, grid):
if(x==self.max_x and y==self.max_y):
self.ways=self.ways+1 if(x<self.max_x and grid[y][x+1]!=1):
self.find_ways(x+1, y, grid)
if(y<self.max_y and grid[y+1][x]!=1):
self.find_ways(x, y+1, grid) # @obstacleGrid is a grid of m*n cells
def uniquePathsWithObstaclesDP(self, obstacleGrid):
m = len(obstacleGrid)
if(m ==0): return 0
n = len(obstacleGrid[0])
if(obstacleGrid[0][0] ==1): return 0 max_ways={}
for x in range(n):max_ways[x]=0 max_ways[0]=1;
for y in range(m):
for x in range(n):
if(obstacleGrid[y][x] ==1):
max_ways[x]=0
else:
if(x >0):
max_ways[x] = max_ways[x-1]+max_ways[x]
return max_ways[n-1]; if __name__ == '__main__':
sl=Solution()
grid=[[0,0,0],
[0,1,0],
[0,0,0]]
print sl.uniquePathsWithObstacles(grid)
grid=[[0,0,0,0,0],
[0,1,0,0,0],
[0,1,0,0,0],
[0,1,0,0,0],
[0,0,0,0,0]]
print sl.uniquePathsWithObstacles(grid)
grid= [
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,1,0,0,0,0,0,0,0,0]
] print sl.uniquePathsWithObstacles(grid)
grid= [
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0] ,
[0,0,0,0,0,0,0,0,0,0] ,
[0,0,0,0,0,0,0,0,0,0] ,
[0,0,0,0,0,0,0,0,0,0]
] print sl.uniquePathsWithObstacles(grid)
grid=[
[0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0],
[1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,1],
[0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0],
[0,0,0,1,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0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,0,0,0,0,1,1],
[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0]
]
print sl.uniquePathsWithObstaclesDP(grid)

LEETCODE —— Unique Paths II [Dynamic Programming]的更多相关文章

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

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

  2. LeetCode: Unique Paths II 解题报告

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

  3. [LeetCode] Unique Paths II 不同的路径之二

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

  4. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  5. Leetcode Unique Paths II

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

  6. [Leetcode] unique paths ii 独特路径

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

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

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

  9. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

随机推荐

  1. 【2016蓝桥杯省赛】试题C++ B组试题

    一.    煤球数目 作答:171700 #include <iostream> using namespace std; int main() { ,x=; ;i<=;i++){ ...

  2. select、poll 和epoll区别

    阻塞 I/O(blocking IO) 当用户进程调用了recvfrom这个系统调用,kernel就开始了IO的第一个阶段:准备数据(对于网络IO来说,很多时候数据在一开始还没有到达.比如,还没有收到 ...

  3. Google 地图切片URL地址解析

    一.Google地图切片的投影方式及瓦片索引机制 1.地图投影 Google地图采用的是Web墨卡托投影(如下图),为了方便忽略了两极变形较大的地区,把世界地图做成了一个边长等于赤道周长的正方形(赤道 ...

  4. python开发_stat

    当我们使用os.stat(path)获取一个文件(夹)信息的时候, os.stat(path)本身返回的是一个元组如: nt.stat_result(st_mode=33206, st_ino=203 ...

  5. js判断设备信息,安卓、ios、还是pc端

    前端开发获取设备信息的代码if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { window.location.href =" ...

  6. struts2框架 转载 精华帖

    一.Struts2简介 参考<JavaEE 轻量级框架应用与开发—S2SH> Struts框架是流行广泛的一个MVC开源实现,而Struts2是Struts框架的新一代产品,是将Strut ...

  7. 【PHP开发规范】老生常谈的编码开发规范你懂多少?

    [PHP开发规范]老生常谈的编码开发规范你懂多少? 这几天看了一下阿里技术发布的一套Java开发规范<阿里巴巴Java开发手册>,里面写了阿里内部的Java开发规范标准,写的很好.这套Ja ...

  8. CTF-i春秋网鼎杯第二场misc部分writeup

    CTF-i春秋网鼎杯第二场misc部分writeup 套娃 下载下来是六张图片 直接看并没有什么信息 一个一个查看属性 没有找到有用信息 到winhexv里看一下 都是标准的png图片,而且没有fla ...

  9. 筑基期—C语言

    1.1 环境: 在ANSIC的任何一种是实现中,存在两种不同的环境.第一种是翻译环境,第二种是执行环境.标准明确说明这两种环境不必在同一台机器上,交叉编译器就是在一台机器上运行,但它所产生的可执行代码 ...

  10. spark本地环境的搭建到运行第一个spark程序

    搭建spark本地环境 搭建Java环境 (1)到官网下载JDK 官网链接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...