唯一路径问题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.

  1. [
  2. [0,0,0],
  3. [0,1,0],
  4. [0,0,0]
  5. ]

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),即满足子问题重叠性质,因此使用动态规划可以获得更好的效率

 
 
  1. '''
  2. Created on Nov 25, 2014
  3.  
  4. @author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
  5. '''
  6. class Solution:
  7. def __init__(self):
  8. self.ways=0
  9. self.max_x=0
  10. self.max_y=0
  11.  
  12. # @param obstacleGrid, a list of lists of integers
  13. # @return an integer
  14. def uniquePathsWithObstacles(self, obstacleGrid):
  15. if(obstacleGrid==None):return 0
  16. if(len(obstacleGrid)==0):return 0
  17. if(obstacleGrid[0][0] ==1): return 0
  18.  
  19. self.__init__()
  20. self.max_x=len(obstacleGrid[0])-1
  21. self.max_y=len(obstacleGrid)-1
  22. self.find_ways(0,0, obstacleGrid)
  23. return self.ways
  24.  
  25. def find_ways(self, x, y, grid):
  26. if(x==self.max_x and y==self.max_y):
  27. self.ways=self.ways+1
  28.  
  29. if(x<self.max_x and grid[y][x+1]!=1):
  30. self.find_ways(x+1, y, grid)
  31. if(y<self.max_y and grid[y+1][x]!=1):
  32. self.find_ways(x, y+1, grid)
  33.  
  34. # @obstacleGrid is a grid of m*n cells
  35. def uniquePathsWithObstaclesDP(self, obstacleGrid):
  36. m = len(obstacleGrid)
  37. if(m ==0): return 0
  38. n = len(obstacleGrid[0])
  39. if(obstacleGrid[0][0] ==1): return 0
  40.  
  41. max_ways={}
  42. for x in range(n):max_ways[x]=0
  43.  
  44. max_ways[0]=1;
  45. for y in range(m):
  46. for x in range(n):
  47. if(obstacleGrid[y][x] ==1):
  48. max_ways[x]=0
  49. else:
  50. if(x >0):
  51. max_ways[x] = max_ways[x-1]+max_ways[x]
  52. return max_ways[n-1];
  53.  
  54. if __name__ == '__main__':
  55. sl=Solution()
  56. grid=[[0,0,0],
  57. [0,1,0],
  58. [0,0,0]]
  59. print sl.uniquePathsWithObstacles(grid)
  60. grid=[[0,0,0,0,0],
  61. [0,1,0,0,0],
  62. [0,1,0,0,0],
  63. [0,1,0,0,0],
  64. [0,0,0,0,0]]
  65. print sl.uniquePathsWithObstacles(grid)
  66. grid= [
  67. [0,0,0,0,0,0,0,0,0,0],
  68. [0,0,0,0,0,1,0,0,0,0],
  69. [0,0,0,0,0,0,0,0,0,0],
  70. [0,0,0,0,0,0,0,0,0,0],
  71. [0,1,0,0,0,0,0,0,0,0],
  72. [0,0,0,0,0,0,0,0,0,0],
  73. [0,1,0,0,0,0,0,0,0,0]
  74. ]
  75.  
  76. print sl.uniquePathsWithObstacles(grid)
  77. grid= [
  78. [0,0,0,0,0,0,0,0,0,0],
  79. [0,0,0,0,0,0,0,0,0,0],
  80. [0,0,0,0,0,0,0,0,0,0],
  81. [0,0,0,0,0,0,0,0,0,0],
  82. [0,0,0,0,0,0,0,0,0,0],
  83. [0,0,0,0,0,0,0,0,0,0],
  84. [0,0,0,0,0,0,0,0,0,0],
  85. [0,0,0,0,0,0,0,0,0,0] ,
  86. [0,0,0,0,0,0,0,0,0,0] ,
  87. [0,0,0,0,0,0,0,0,0,0] ,
  88. [0,0,0,0,0,0,0,0,0,0]
  89. ]
  90.  
  91. print sl.uniquePathsWithObstacles(grid)
  92. grid=[
  93. [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],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,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  109. [0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0],
  110. [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],
  111. [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],
  112. [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],
  113. [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],
  114. [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]
  115. ]
  116. 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. etherlime-4-Etherlime CLI

    Etherlime CLI命令行界面 Installing & Help Syntax语法 npm i -g etherlime Install the global etherlime to ...

  2. windows下安装redis并设置自启动

    一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的windows版本的,现在官网以及没有下载地址,只能在github上下载,官网只提供linux版本的下载 官网下载地址 ...

  3. 在handlebars.js {{#if}}条件下的逻辑运算符解决方案

    解决方案.这增加了比较运算符. Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) { switch (o ...

  4. [图解tensorflow源码] MatMul 矩阵乘积运算 (前向计算,反向梯度计算)

  5. c++赋值运算符为什么要返回引用?

    为什么赋值运算符要返回引用?? 因为赋值操作会改变左值,而 + 之类的运算符不会改变操作数,所以说赋值运算符重载要返回引用以用于类似 (a=b)=c 这样的再次对a=b进行写操作的表达式.+ 返回一个 ...

  6. 实现点击到底部、顶部、指定div功能

    顶部: $(".back_top").click(function () { scrollTo(0, 0); }); function goTop() { $('html, bod ...

  7. jQuery 插件运用

    1. fullpage 插件(全屏) 官网:http://www.jq22.com/ jqueryui 官网:http://jqueryui.com/draggable/ 1.1 使用方法 引入文件 ...

  8. 关于IRAM和IFLASH启动模式,重映射remap 整理中

    工程基于NXP LPC2468 1 为什么试用IRAM MODE 2 设置Program algorithm 编程算法的作用是什么 3 IRAM和FLASH 模式下IROM和IRAM的地址为什么不一样 ...

  9. Glide Golang包管理

    Golang的包管理乱得不行,各种工具横空出世,各显神通啊.用了几个下来,发现 Glide 是比较好用的,使用了 vender 来进行管理,多个开发环境的版本不冲突,功能强大,配置文件也足够简单. 初 ...

  10. UEditor富文本web编辑器

    UEditor是由百度web前端研发部开发所见即所得,前几天把公司原来的富文本编辑器换成百度的了,可以把秀米制作的样式 整个复制到文本编辑器中,原汁原味... 到官网看了文档,其实很简单,就简单的配置 ...