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 ...
随机推荐
- @RequestBody, @ResponseBody 注解详解
简介: @RequestBody 作用: i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对 ...
- 使用Memory Analyzer tool(MAT)分析内存泄漏(一)
转载自:http://www.blogjava.net/rosen/archive/2010/05/21/321575.html 前言 在平时工作过程中,有时会遇到OutOfMemoryError,我 ...
- 补丁vs错误(codevs 2218 错误答案)
题目描述 Description 错误就是人们所说的Bug.用户在使用软件时总是希望其错误越少越好,最好是没有错误的.但是推出一个没有错误的软件几乎不可能,所以很多软件公司都在疯狂地发放补丁(有时这种 ...
- Android之webView入门
WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient setWebClient:主要 ...
- 查看Linus中自带的jdk ,设置JAVA_HOME
在配置hadoop是,进行格式化hadoop的时候,出现找不到jdk 我用Red hat是32位的,没有现成的32位的,敲java , 发现本机有java ,就找了一下其位置 找到了jdk-1.6.0 ...
- codeigniter 对数据库的常用操作
codeigniter (CI)是一个优秀.敏捷的PHP开源框架,尤其封装了对数据库的操作,很方便,以下是php ci常用的数据库操作,作个记录: /* ======================= ...
- 用VMware 11.0虚拟机安装Win8 系统失败,提示“shsucdx can't install”
研究了好久,网上那些更改bios的方法根本行不通,因为该版本的biso根本没有SATA选项!解决方法很简单,如下图: 重启虚拟机即可.
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- 【bzoj1066】[SCOI2007]蜥蜴 网络最大流
[bzoj1066][SCOI2007]蜥蜴 Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的 ...
- BZOJ2102 : [Usaco2010 Dec]The Trough Game
暴力枚举答案然后检验. #include<cstdio> int n,m,i,j,k,a[100],b[100],cnt,ans;char s[20]; int main(){ for(s ...