62. Unique Paths(中等,我自己解出的第一道 DP 题^^)
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?
有个图来着,Markdown贴起来很麻烦.不贴了.
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: \(m\) and \(n\) will be at most 100.
解题思路,没啥可说, 用 dynamic programming.
步骤:
step 1: 看 https://mp.weixin.qq.com/s/0AgJmQNYAKzVOyigXiKQhA, 动态规划入门最好材料,没有之一! 10分钟看完,然后第二步.
step 2: 画图自己做这个题.^^
主要是先建模型,既,分析出:
- 状态转移公式,本题有3个状态转移公式,分别对应这i,j条件.具体参考最下方的代码(简单递归,当然无法执行,因为时间复杂度为 \(O(2^n)\), 但能很好的展示了转移公式);
- 最优子结构(分别对应各自的状态转移公式);
- 边界,既
F[0,0] = 1.
自己想法,自个代码^^:
\(O(m*n)\) time, \(O(n)\) extra space.
class Solution {
public:
// 真正的DP求解
// $O(m*n)$ time, $O(n)$ extra space.
// 时间复杂度无法再小了,但空间复杂度还可以再小.
// space complexity 最低可为 min(m, n);
// 听说有 $O(1)$ 的空间复杂度?
// 如果不用 DP, 可用 math 的方法,如下:
// https://leetcode.com/problems/unique-paths/discuss/
int uniquePaths(int m, int n) {
if(m == 1 || n == 1) return 1;
if(m < n) return uniquePaths(n, m);
vector<int> temp(n - 1, 0);
for(int i = 1; i < m; i++){
for(int j = 1; j < n; j++){
if(i == 1 && j == 1) temp[0] = 2;
else if(i == 1 && j > 1) temp[j - 1] = 1 + temp[j - 2];
else if(i > 1 && j == 1) temp[0] = temp[0] + 1;
else if(i > 1 && j > 1) temp[j - 1] = temp[j - 1] + temp[j - 2];
}
}
return temp[n - 2];
}
};
下面是简单递归,但 time complexity \(O(2^n)\), 太高了.
下面代码可清晰地展示出DP的三要素:
状态转移公式、最优子结构和边界.
class Solution {
public:
// 方法一: 简单递归,但 time complexity $O(2^n)$, 太高了.
int uniquePaths(int m, int n) {
int i = m - 1, j = n - 1;
if(i == 0 && j == 0) return 1;
else if(i == 0 && j != 0) return uniquePaths(i, j - 1);
else if(j == 0 && i != 0) return uniquePaths(i - 1, j);
else if(j > 0 && i > 0) return uniquePaths(i, j - 1) + uniquePaths(i - 1, j);
}
};
随机推荐
- OAuth2.0学习(1-9)新浪开放平台微博认证-web应用授权(授权码方式)
1. 引导需要授权的用户到如下地址: URL 1 https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&respons ...
- HTML mate标签
META标签分两大部分:HTTP标题信息(http-equiv)和页面描述信息(name). http-equiv http-equiv类似于HTTP的头部协议,它回应给浏览器一些有用的信息,以帮助正 ...
- django关闭调试信息,打开内置错误视图
1 内置错误视图 Django内置处理HTTP错误的视图,主要错误及视图包括: 404错误:page not found视图 500错误:server error视图 400错误:bad reques ...
- django模版中配置和使用静态文件方法
1 在项目根路径下创建静态文件目录static: test3 是我的项目名,根目录下面: booktest 是一个应用 static是我建立的静态文件目录(这个名字其实可以叫其他的),静态文件都放在这 ...
- API验证及AES加密
API验证 API验证: a. 发令牌: 静态 PS: 隐患 key被别人获取 b. 动态令牌 PS: (问题越严重)用户生成的每个令牌被黑客获取到,都会破解 c. 高级版本 PS: 黑客网速快,会窃 ...
- Spring(六):Spring&Struts2&Hibernate搭建的blog项目
经过断断续续的学习.累积,终于基于别人的开源blog项目,变成了自己的第一个相对完整点的blog项目. 计划暂时把这个blog程序暂停------有更多(工作中用到的)东西需要去做,因此学习SSH b ...
- [转]scrapy中的request.meta
作者:知乎用户链接:https://www.zhihu.com/question/54773510/answer/146971644 meta属性是字典,字典格式即{'key':'value'},字典 ...
- scrapy安装教程
Step 1 •安装Python2.7(32位版本) –https://www.python.org/downloads/release/python-279/ Setp 2 •打开"运行& ...
- Java程序优化之替换swtich
关键字switch语句用于多条件判断,功能类似于if-else语句,两者性能也差不多,不能说switch会降低系统性能.在绝大部门情况下,switch语句还是有性能提升空间的. 但是在项目代码中,如果 ...
- [原创软件]Maya报错窗口监测器
软件主要功能: 监测Maya软件运行状态,如弹出报错窗口,则自动点击关闭 程序界面截图: 开发环境及语言: c# .NET Framework 4.0 Visual Studio 2015 更新日志: ...