LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
2014-11-25 18:10
295 查看
唯一路径问题II
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
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
The total number of unique paths is
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),即满足子问题重叠性质,因此使用动态规划可以获得更好的效率。
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
1and
0respectively 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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0,0,1,1,0,0,1,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,1,0,1,0,0,0,0,1,1], [0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,1], [1,1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,1,1,0,0,0,0,0,0,0,0,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(动态规划)
- leetcode做题总结,动态规划I(Triangle,Unique PathsI/II,Minimum Path Sum,Climbing Stairs,Jump Game,Word Break)
- leetcode_63题——Unique Paths II(动态规划)
- Leetcode 动态规划 Unique Paths II
- 【LeetCode练习题】Unique Paths II
- Leetcode(62)(63) Unique Paths I II
- [Leetcode] unique paths ii 独特路径
- LeetCode 63: Unique Paths II
- 【LeetCode-面试算法经典-Java实现】【063-Unique Paths II(唯一路径问题II)】
- (leetcode)Unique Paths II
- leetcode Unique Paths II
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
- [LeetCode][Java] Unique Paths II
- [LeetCode] Unique Paths II
- LeetCode_OJ【63】Unique Paths II
- 【LeetCode】C# 63、Unique Paths II
- LeetCode--No.63--Unique Paths II
- LeetCode题库解答与分析——#63.不同路径IIUniquePathsII
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
- LeetCode Unique Paths II