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],
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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 [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 ...
随机推荐
- etherlime-4-Etherlime CLI
Etherlime CLI命令行界面 Installing & Help Syntax语法 npm i -g etherlime Install the global etherlime to ...
- windows下安装redis并设置自启动
一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的windows版本的,现在官网以及没有下载地址,只能在github上下载,官网只提供linux版本的下载 官网下载地址 ...
- 在handlebars.js {{#if}}条件下的逻辑运算符解决方案
解决方案.这增加了比较运算符. Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) { switch (o ...
- [图解tensorflow源码] MatMul 矩阵乘积运算 (前向计算,反向梯度计算)
- c++赋值运算符为什么要返回引用?
为什么赋值运算符要返回引用?? 因为赋值操作会改变左值,而 + 之类的运算符不会改变操作数,所以说赋值运算符重载要返回引用以用于类似 (a=b)=c 这样的再次对a=b进行写操作的表达式.+ 返回一个 ...
- 实现点击到底部、顶部、指定div功能
顶部: $(".back_top").click(function () { scrollTo(0, 0); }); function goTop() { $('html, bod ...
- jQuery 插件运用
1. fullpage 插件(全屏) 官网:http://www.jq22.com/ jqueryui 官网:http://jqueryui.com/draggable/ 1.1 使用方法 引入文件 ...
- 关于IRAM和IFLASH启动模式,重映射remap 整理中
工程基于NXP LPC2468 1 为什么试用IRAM MODE 2 设置Program algorithm 编程算法的作用是什么 3 IRAM和FLASH 模式下IROM和IRAM的地址为什么不一样 ...
- Glide Golang包管理
Golang的包管理乱得不行,各种工具横空出世,各显神通啊.用了几个下来,发现 Glide 是比较好用的,使用了 vender 来进行管理,多个开发环境的版本不冲突,功能强大,配置文件也足够简单. 初 ...
- UEditor富文本web编辑器
UEditor是由百度web前端研发部开发所见即所得,前几天把公司原来的富文本编辑器换成百度的了,可以把秀米制作的样式 整个复制到文本编辑器中,原汁原味... 到官网看了文档,其实很简单,就简单的配置 ...