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. JDBC的基本步骤

    JDBC全名是Java Data Base Connectivity就是Java数据库连接,这是Java用于向数据库执行SQL语句的API,JDBC可以为多种关系型数据库提供统一的访问,而不用考虑细节 ...

  2. Java for LeetCode 151 Reverse Words in a String

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  3. cocos2dx实现象棋之运动

    1.头文件 void moveStone(int moveid, int killid, int x, int y); void moveComplete(CCNode*, void*); bool ...

  4. Material Design综合实例

    背景知识 drawlayout的使用 recycleView的使用 CardView的使用 一些开源动画库的使用 ImageView的scaleType属性与adjustViewBounds属性 ,参 ...

  5. centos 截图命令 screenshot

    [root@ok ~]# gnome-screenshot#全屏截图 [root@ok ~]# gnome-screenshot --interactive#自定义截图

  6. 在Android中让Preference的宽度占满整个屏幕的宽度

    今天遇到一个问题,需要修改Preference的宽度,让其与屏幕宽度一致.搞了一上午. 终于发现Preference的这个尺寸是在PreferenceFrameLayout中设置的.通过下面这段代码, ...

  7. UML中的图的出现顺序

    上接:UML从需求到设计--用例 从开始接触UML到现在对UML逐渐有了更深入的了解.刚开始,对于UML总是感觉UML就是图.一提起UML 就想着这个是画图的东西. 具体这些图都是干什么的.为什么会有 ...

  8. go编写简单的web服务器

    package main import ( "fmt" "log" "net/http" "strings" ) //h ...

  9. Android快捷键

    Android快捷键ALT+/ :在布局文件中,提示输入的内容Shift + Ctrl +  / :注释Shift + Ctrl +  \ :解除注释

  10. Effective C++笔记:设计与声明

    条款18:让接口容易被正确使用,不易被误用 1,好的接口很容易被正确使用,不容易被误用.你应该在你的所有接口中努力达成这些性质. 2,“促进正使用”的办法包括接口的一致性,以及与内置类型的行为兼容. ...