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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,0,0,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 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [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 ...
- 63. Unique Paths II 动态规划
description: https://leetcode.com/problems/unique-paths/ 机器人从一堆方格的左上角走到右下角,只能往右或者往下走 ,问有几种走法,这个加了难度, ...
- [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) 有关 这种情况下,时间 ...
随机推荐
- c语言-四阶龙格-库塔法
#include<stdio.h> #include<math.h> #define n 14 //double func1(double x, double y); doub ...
- 编译内核实现iptables防火墙layer7应用层过滤 (三)
在前面的两篇文章中我们主要讲解了Linux防火墙iptables的原理及配置规则,想博友们也都知道iptables防火墙是工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙.以 ...
- SQLServer - 约束
一.约束的分类 在SQLServer中,有3种不同类型的约束. 1.实体约束 实体约束是关于行的,比如某一行出现的值就不允许出现在其他行,例如主键. 2.域约束 域约束是关于列的,对于所有行,某一列有 ...
- CnBlog客户端Windows Live Write安装方法
官方帮助http://space.cnblogs.com/forum/topic/8550 注:如果自动配置没有成功,需要手动配置: a) 在"Type of weblog that yo ...
- Jquery实现select左右栏的添加移除
首先是效果展示, 兼容火狐,IE6+,谷歌没测试有
- HyperV采用硬盘拷贝的方式迁移虚拟机后的问题处理
公司有一台RSA认证服务器,是在windows 2008 R2下的虚拟机,最近总是出现服务中断的情况,考虑到宿主机性能较差,于是想迁移到新的服务器中. 本想通过SCVMM来迁移,但因功能不可用,所以采 ...
- Android 与Unity交互之Toast消息
Toast.makeText(MainActivity.this,message.obj.toString(),Toast.LENGTH_SHORT).show();这一句代码不能直接放在 publi ...
- IntelliJ IDEA手工安python装插件方法
IntelliJ IDEA手工安装插件方法 以IntelliJ IDEA 11.0.1安装python为例: (1)下载python插件地址:http://plugins.intellij.net/p ...
- struts2乱码
在spring.jar包的org.springframework.web.filter包下有个CharacterEncodingFilter.java 把spring.jar放进工程的lib里,然后在 ...
- 【转】C#线程同步示例
using System; using System.Threading; // 银行帐户类 class Account { int balance; ...