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 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【摘】的更多相关文章
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [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 ...
- 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 ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- [leetcode]Unique Paths @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...
- [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 ...
- Leetcode Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
随机推荐
- Lubuntu下配置Python开发环境
安装完系统必须要做的几件事: 一.更新软件 (如果是通过最新版镜像安装,可无视此步骤) 1.选择速度比较快的源,默认的源速度不一定快.二.配置终端 1.设置终端背景,前景色,透明度 ...
- JavaScript、Jquery选择题
尚学堂Java EE软件工程师认证考试 试题库-选择题 一. 选择题(包括单选和双选) 1.B 在JavaScript中,以下变量命名非法的是( )(选择一项) A. numb_1 ...
- Win8 Cisco VPN Client 442错误解决办法
进入注册表regedit,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\CVirtA找到DisplayName, x86系统的将值" ...
- js prototype
1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个类B,这个类是以A为原型的,并且能进行扩展.我们称B的原型为A. 2 javasc ...
- STL —— STL六大组件
注:以下内容摘自 http://blog.csdn.net/byxdaz/article/details/4633826 STL六大组件 容器(Container) 算法(Algorithm) 迭代器 ...
- Java Hour 45 Hibernate ( 2 )
基本确定了,一个月后也就是在2014年的开端,我将离开这个公司. 所以我大概还有30个学时. 45.1 你需要一个数据库 首先,必须有一个试验用的数据库,这里我们使用MySQL. 尽管书中的说明是使用 ...
- Ps 之路 更改前景色
用快速选中工具 选中要改变的图片,选中你喜欢的颜色,按Alt+Del
- tnsnames.ora存放路径
tnsnames.ora存放路径: D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN
- UVA 1328 - Period KMP
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36131 题意:给出一个长度为n的字符串,要求找到一些i,满足说从1 ...
- loj 1046(bfs)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26766 思路:由于数据不是很大,我们可以枚举骑士最后聚集的位置,然 ...