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?

最简单的回溯:

 int backtrack(int r, int c, int m, int n) {
if (r == m && c == n)
return ;
if (r > m || c > n)
return ; return backtrack(r+, c, m, n) + backtrack(r, c+, m, n);
}

用一个表记录状态,优化:

 const int M_MAX = ;
const int N_MAX = ; int backtrack(int r, int c, int m, int n, int mat[][N_MAX+]) {
if (r == m && c == n)
return ;
if (r > m || c > n)
return ; if (mat[r+][c] == -)
mat[r+][c] = backtrack(r+, c, m, n, mat);
if (mat[r][c+] == -)
mat[r][c+] = backtrack(r, c+, m, n, mat); return mat[r+][c] + mat[r][c+];
} int bt(int m, int n) {
int mat[M_MAX+][N_MAX+];
for (int i = ; i < M_MAX+; i++) {
for (int j = ; j < N_MAX+; j++) {
mat[i][j] = -;
}
}
return backtrack(, , m, n, mat);
}

动态规划,从最靠近终点的地方(子问题)开始算:

 const int M_MAX = ;
const int N_MAX = ; int dp(int m, int n) {
int mat[M_MAX+][N_MAX+] = {};
mat[m][n+] = ; for (int r = m; r >= ; r--)
for (int c = n; c >= ; c--)
mat[r][c] = mat[r+][c] + mat[r][c+]; return mat[][];
}

这里的空间可以进一步优化,因为最底行计算完之后可以直接用来计算上一行。

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

另外,这道题其实也是一个组合数学的题,结果就是C(m + n - 2, n - 1)。这里要注意计算是overflow。

 int gcd(int a, int b) {
while(b) {
int c = a%b;
a = b;
b = c;
}
return a;
} int C(int m, int n) {
if(m - n < n) {
n = m - n;
}
int result = ;
for(int i = ; i <= n; i++) {
int mult = m;
int divi = i;
int g = gcd(mult,divi);
mult /= g;
divi /= g;
result /= divi;
result *= mult;
m--;
}
return result;
}

这道题真是相当经典。

LeetCode | Unique Paths【摘】的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

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

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

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

  3. [LeetCode] Unique Paths 不同的路径

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

  4. 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  ...

  5. [leetcode]Unique Paths II @ Python

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

  6. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  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. 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 [动态规划 Dynamic Programming]

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

随机推荐

  1. 集群ssh服务和免密码登录的配置

    安装Hadoop之前,由于集群中大量主机进行分布式计算需要相互进行数据通信,服务器之间的连接需要通过ssh来进行,所以要安装ssh服务,默认情况下通过ssh登录服务器需要输入用户名和密码进行连接,如果 ...

  2. Windows下尝试PHP7提示丢失VCRUNTIME140.DLL的问题解决

    前天PHP7.0.0正式版发布了,有一些比较好的改进,官方也说速度比php5.6快了两倍,性能上有了很大提升,并且也发布了从php5.x向php7迁移的问题,所以今后php网站迁移后能够大幅度的提升网 ...

  3. codeforces B. Petya and Staircases 解题报告

    题目链接:http://codeforces.com/problemset/problem/362/B 题目意思:给出整数n和m,表示有n级楼梯和m级dirty的楼梯,接下来m个数表示对应是哪一个数字 ...

  4. UVALive 7270 Osu! Master (阅读理解题)

    题目:传送门. 题意:阅读理解题,是一个osu的游戏,问得分.把题目翻译过来就是如果出现S或者BC后面跟的是1,ans就加1. #include <iostream> #include & ...

  5. HDU1009老鼠的旅行 (贪心算法)

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. 用Navicat for oracle导入现有数据库

    重要参考:http://www.admin5.com/article/20130420/499241.shtml 前期准备,参考另一篇随笔. 安装Oracle,PL/SQL, 新建表空间myTS,新建 ...

  7. July 20th, Week 30th Wednesday, 2016

    Learn from yesterday, live for today, and hope for tomorrow. 借鉴昨天,活着当下,憧憬未来. Yesterday is the past, ...

  8. 《数学之美》(吴军 著)读书笔记:第1章 文字和语言 vs 数字和信息

    第1章有4个小节,以及前言. 前言 1.信息 2.文字和数字 3.文字和语言背后的数学 4.小结 下面我一一展开,让我们看看每一节都说了什么. 前言 语言和数字都是信息传播的载体,他们之间其实存在着天 ...

  9. 记VS2013并行编译导致出错的解决过程

    接前一篇,电脑换了新的,系统是64bit的win8系统,先安装了SQLServer2012,再安装VS2010旗舰版,Stop!为什么还是2010?因为2010太经典了,以至于公司的项目还在用它写项目 ...

  10. 13、在 uwp应用中,给图片添加高斯模糊滤镜效果(一)

    如果在应用中,如果想要给app 添加模糊滤镜,可能第一想到的是第三方类库,比如 Win2d.lumia Imaging SDK .WriteableBitmapEx,不可否认,这些类库功能强大,效果也 ...