博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode:Unique Paths I II
阅读量:4484 次
发布时间:2019-06-08

本文共 3208 字,大约阅读时间需要 10 分钟。

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

算法1:最容易想到的是递归解法,uniquePaths(m, n) = uniquePaths(m, n-1) + uniquePaths(m-1, n), 递归结束条件是m或n等于1,这个方法oj超时了

1 class Solution {2 public:3     int uniquePaths(int m, int n) {4         if(m == 1 || n == 1)return 1;5         else return  uniquePaths(m, n - 1) + uniquePaths(m - 1, n);6     }7 };

算法2:动态规划,算法1的递归解法中,其实我们计算了很多重复的子问题,比如计算uniquePaths(4, 5) 和 uniquePaths(5, 3)时都要计算子问题uniquePaths(3, 2),再者由于uniquePaths(m, n) = uniquePaths(n, m),这也使得许多子问题被重复计算了。要保存子问题的状态,这样很自然的就想到了动态规划方法,设dp[i][j] = uniquePaths(i, j), 那么动态规划方程为:

  • dp[i][j] = dp[i-1][j] + dp[i][j-1]
  • 边界条件:dp[i][1] = 1, dp[1][j] = 1
1 class Solution { 2 public: 3     int uniquePaths(int m, int n) { 4         vector
> dp(m+1, vector
(n+1, 1)); 5 for(int i = 2; i <= m; i++) 6 for(int j = 2; j <= n; j++) 7 dp[i][j] = dp[i-1][j] + dp[i][j-1]; 8 return dp[m][n]; 9 }10 };

上述过程其实是从左上角开始,逐行计算到达每个格子的路线数目,由递推公式可以看出,到达当前格子的路线数目和两个格子有关:1、上一行同列格子的路线数目;2、同一行上一列格子的路线数目。据此我们可以优化上面动态规划方法的空间:

1 class Solution { 2 public: 3     int uniquePaths(int m, int n) { 4         vector
dp(n+1, 1); 5 for(int i = 2; i <= m; i++) 6 for(int j = 2; j <= n; j++) 7 dp[j] = dp[j] + dp[j-1]; 8 return dp[n]; 9 }10 };

算法3:其实这个和组合数有关,对于m*n的网格,从左上角走到右下角,总共需要走m+n-2步,其中必定有m-1步是朝右走,n-1步是朝下走,那么这个问题的答案就是组合数:, 这里需要注意的是求组合数时防止乘法溢出        

1 class Solution { 2 public: 3     int uniquePaths(int m, int n) { 4         return combination(m+n-2, m-1); 5     } 6      7     int combination(int a, int b) 8     { 9         if(b > (a >> 1))b = a - b;10         long long res = 1;11         for(int i = 1; i <= b; i++)12             res = res * (a - i + 1) / i;13         return res;14     }15 };

 


 

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.

这一题可以完全采用和上一题一样的解法,只是需要注意dp的初始化值,和循环的起始值

1 class Solution { 2 public: 3     int uniquePathsWithObstacles(vector
> &obstacleGrid) { 4 int m = obstacleGrid.size(), n = obstacleGrid[0].size(); 5 vector
dp(n+1, 0); 6 dp[1] = (obstacleGrid[0][0] == 0) ? 1 : 0; 7 for(int i = 1; i <= m; i++) 8 for(int j = 1; j <= n; j++) 9 if(obstacleGrid[i-1][j-1] == 0)10 dp[j] = dp[j] + dp[j-1];11 else dp[j] = 0;12 return dp[n];13 }14 };

 

【版权声明】转载请注明出处:

转载于:https://www.cnblogs.com/TenosDoIt/p/3704091.html

你可能感兴趣的文章
java 布局教程_java布局学习(新)
查看>>
你真的会写Java吗?
查看>>
alibaba.fastjson.JSONObject 解析
查看>>
终于有人把Elasticsearch原理讲透了
查看>>
Java使用POI 读取和写入Excel指南
查看>>
shell脚本中各类括号的作用(小结)
查看>>
借用Snippet插件美化博客中的代码
查看>>
深入研究java.lang.Runtime类
查看>>
10677 我们仍未知道那天所看见的花的名字
查看>>
ScanTailor-ScanTailor 自动矫正图像歪斜
查看>>
UVA GCD - Extreme (II)
查看>>
完成个人中心—导航标签
查看>>
前端性能优化
查看>>
static
查看>>
属性动画
查看>>
Hadoop集群时钟同步
查看>>
C++二维数组讲解、二维数组的声明和初始化
查看>>
纹理映射和混合
查看>>
PHP获取域名、IP地址的方法
查看>>
php验证复选框的小例子
查看>>