Unique Paths

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超时了

 class Solution {
public:
int uniquePaths(int m, int n) {
if(m == || n == )return ;
else return uniquePaths(m, n - ) + uniquePaths(m - , n);
}
};

算法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
 class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > dp(m+, vector<int>(n+, ));
for(int i = ; i <= m; i++)
for(int j = ; j <= n; j++)
dp[i][j] = dp[i-][j] + dp[i][j-];
return dp[m][n];
}
};

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

 class Solution {
public:
int uniquePaths(int m, int n) {
vector<int>dp(n+, );
for(int i = ; i <= m; i++)
for(int j = ; j <= n; j++)
dp[j] = dp[j] + dp[j-];
return dp[n];
}
};

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

 class Solution {
public:
int uniquePaths(int m, int n) {
return combination(m+n-, m-);
} int combination(int a, int b)
{
if(b > (a >> ))b = a - b;
long long res = ;
for(int i = ; i <= b; i++)
res = res * (a - i + ) / i;
return res;
}
};

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.

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

 class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {
int m = obstacleGrid.size(), n = obstacleGrid[].size();
vector<int>dp(n+, );
dp[] = (obstacleGrid[][] == ) ? : ;
for(int i = ; i <= m; i++)
for(int j = ; j <= n; j++)
if(obstacleGrid[i-][j-] == )
dp[j] = dp[j] + dp[j-];
else dp[j] = ;
return dp[n];
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3704091.html

LeetCode:Unique Paths I II的更多相关文章

  1. LeetCode: Unique Paths I & II & Minimum Path Sum

    Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m  ...

  2. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  3. [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 ...

  4. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  5. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  6. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  7. LEETCODE —— Unique Paths II [Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  8. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. [Leetcode] unique paths ii 独特路径

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

随机推荐

  1. 关于配置并发访问的服务器apache、nginx

    一. apache,nginx比较     关于Apache与Nginx的优势比较  (apache计算密集型   nginx io密集型  各有优势,不存在谁取代谁) 二.nginx 基于nginx ...

  2. 数据库性能调优——sql语句优化(转载及整理) —— 篇2

    下面是在网上搜集的一些个人认为比较正确的调优方案,如有错误望指出,定虚心改正 (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中 ...

  3. 日常工作生活中的做人做事道理[持续更新ing]

    1.凡是预则立,不预则废 2.不能用特殊案例说明事情本身的发展规律 3.任务不能拖,需主动出击,想方设法完成 4.工作要有细致化的沟通和安排 5.解决问题和安排任务可以逆向思维的去想 6.问题要举一反 ...

  4. IE无法正常打开QC的解决方案

    方案一: 用兼容视图方式打开.(亲测IE10 可行) 方案二:(使用版本IE6-IE10) 1.安装过程中Jboss服务键入windows系统用户名密码域时总是提示用户名密码不正确! 解决方法:我的电 ...

  5. 方程ax2+bx+c=0;一元二次方程。求根

    <body>方程ax2+bx+c=0;一元二次方程.求根请输入a:<input type="number" id="a"/><br ...

  6. 基于python的flask的应用实例注意事项

    1.所有的html文件均保存在templates文件夹中 2.运行网页时python manage.py runserver

  7. 图解SQL的inner join、left join、right join、full outer join、union、union all的区别

    转自:http://blog.csdn.net/jz20110918/article/details/41806611 假设我们有两张表.Table A 是左边的表.Table B 是右边的表.其各有 ...

  8. pig 介绍与pig版 hello world

    前两天使用pig做ETL,粗浅的看了一下,没有系统地学习,感觉pig还是值得学习的,故又重新看programming pig. 以下是看的第一章的笔记: What is pig? Pig provid ...

  9. Windows事件ID大全

    51 Windows 无法找到网络路径.请确认网络路径正确并且目标计算机不忙或已关闭.如果 Windows 仍然无法找到网络路径,请与网络管理员联系. 52 由于网络上有重名,没有连接.请到“控制面板 ...

  10. HDU 5056 Boring Count --统计

    题解见官方题解,我这里只实现一下,其实官方题解好像有一点问题诶,比如 while( str[startPos] != str[i+1] ) cnt[str[startPos]]--, startPos ...