LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题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,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0,1,0,0,0,1,0,0,0,0,0,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]的更多相关文章
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [Leetcode] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
随机推荐
- 常用的sql语法_Row_Number
可用来分页,也可以用来egg:获取同类型的最新的信息 ROW_NUMBER() 说明:返回结果集分区内行的序列号,每个分区的第一行从1开始.语法:ROW_NUMBER () OVER ([ < ...
- oracle ORA-01722:无效数字 记录
今天在对12万条记录的表进行左联接时,有时可以查询出数据,有时会报无效数字,反复检查,发现问题. 例如sql: SELECT * FROM USER U LEFT JOIN USER_ROLE UR ...
- [Nagios] Error: Template 'timman' specified in contact definition could not be not found (c
Check nagios配置文件报错例如以下: [nagios@2 etc]$ /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios ...
- Rendering Engine 主流的浏览器内核(排版引擎、渲染引擎、解释引擎)有哪几种,分别的特点
一.A web browser engine A rendering engine is software that draws text and images on the screen. The ...
- node vue 开发环境部署时,外部访问页面出现: Invalid Host header 服务器域名访问出现的问题
这是新版本 webpack-dev-server 出于安全考虑, 默认检查 hostname,如果hostname不是配置内的,将中断访问.顾仅存在于开发环境: npm run dev,打包之后不会 ...
- Mongodb基础知识笔记
MongoDB介绍 MongoDB是一个基于分布式文件存储的开源文档数据库.由C++语言编写.旨在为WEB应用提供高性能.高可用性和高伸缩数据存储解决方案. MongoDB优点 MongoDB使用场景 ...
- http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/
http://browniefed.com/blog/2015/09/10/the-shapes-of-react-native/
- 从Oracle向PPAS移行不成功时的处理
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页 回到顶级页面:PostgreSQL索引页 [作者 高健@博客园 luckyjackgao@gmail. ...
- 【转载】C++创建对象的两种方法
原文:http://blog.sina.com.cn/s/blog_586b6c050100dhjg.html 在C++里,有两种方法创建对象: 方法一: ClassName object(param ...
- 3 huigu
w +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 组件复用 --------------- ...