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. iOS支付宝集成时遇到的问题整理(2)

    1.集成支付宝SDK编译报错#include<openssl/asn1.h>这一行  “openssl/asn1.h”file not found 解决方法:在BuildSetting 里 ...

  2. 第八章 了解tempdb数据库

    1.一个sqlserver数据库实例上只能有一个tempdb数据库,这个实例上所有的用户都共享这个数据库.2.tempdb数据库在每次sqlserver重启后都会重新创建,所以数据会丢失.3.因为te ...

  3. 最短路径之迪杰斯特拉(Dijkstra)算法

    迪杰斯特拉(Dijkstra)算法主要是针对没有负值的有向图,求解其中的单一起点到其他顶点的最短路径算法.本文主要总结迪杰斯特拉(Dijkstra)算法的原理和算法流程,最后通过程序实现在一个带权值的 ...

  4. jsp EL 表达式

    EL表达式 EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有E ...

  5. 虚拟机Linux----Ubuntu1204----安装jdk1.8

    1.介绍 这里主要讲一下,如何在Ubuntu1204下通过压缩包的方式安装jdk1.8,rpm的直接运行就行了. 2.步骤 2.1 下载 地址:http://www.oracle.com/techne ...

  6. CI 框架中的自定义路由规则

    在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...

  7. jquery获取复选框(checkbox)的选中值(一组和单个)

    使用jquery获取一组或者单个checkbox的选中状态的值.下面通过一个示例进行说明,假设现有一页面有一组checkbox的name的值为id,那么获取这组name=id的checkbox的值的方 ...

  8. python module getopt usage

    import getopt import sys def usage(): print 'this is a usage.' def main(): try: print sys.argv #sys. ...

  9. java :hello world

    练习java的基本语法. output hellow world. 需求:打包自身项目的bin目录文件为一个临时可运行的jar文件,执行完后删除. 使用process执行jar文件,返回输入流和错误流 ...

  10. poj2387 Til the Cows Come Home 最短路径dijkstra算法

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...