Climbing Stairs爬楼梯——动态规划
题目描写叙述:
初阶:有n层的台阶,一開始你站在第0层,每次能够爬两层或者一层。
请问爬到第n层有多少种不同的方法?
进阶:假设每次能够爬两层。和倒退一层,同一个位置不能反复走,请问爬到第n层有多少种不同的方法?
解题思路:
初阶:一维动态规划。爬楼梯数目事实上是一个斐波拉契数列。
假定f[i] 表示是爬到第i层的方法,那么f[i] = f[i-1] + f[i-2] //第i层的方法数目等于第i-1层数目加上第i-2层数目。
初值:f[0] = 1, f[1] = 1。 //最開始没有爬和第一层的方法数目为1.
输出:f[n] 爬到第n层的方法数目。
实现方法两种:迭代法、递归法
递归法:时间复杂度:O(n) ,空间复杂度:O(n)
迭代法:时间复杂度:O(n),空间复杂度:O(1)
//递归法
class Solution {
public:
/**
* @param n: An integer
* @return: An integer
*/
int climbStairs(int n) {
// write your code here
vector<int> ans(n+1);
ans[0] = 1;
ans[1] = 1;
for(int i=2; i<=n; i++)
ans[i] = ans[i-1] + ans[i-2];
return ans[n];
}
};
//迭代法
class Solution {
public:
/**
* @param n: An integer
* @return: An integer
*/
int climbStairs(int n) {
// write your code here
int prev = 0;
int cur = 1;
for(int i=1; i<=n; i++)
{
int tmp = cur;
cur += prev;
prev = tmp;
}
return cur;
}
};
进阶:參考这里
主要是倒退一层。这个地方可能会违背动态规划无后效性的原则。
那么我们要怎么转化呢?
由条件:同一个位置不能反复走。我们能够知道假设要退步的话,不能退两层以上,由于用两步退两层再一步前进两层,那就会走同样的位置。所以我们最多仅仅能退后一步。
那么题目的条件就能够转换两种情况。
a.跳两层(前进两层)。
b.退一层跳两层 (前进一层)。
1. State:
f[i][0] 表示最后一步是跳两层情况下爬到第i层的方法数目。
f[i][1] 表示最后是一步是退一层跳两层的情况下爬到第i层的方法数目。
2. Function:
f[i+1][1] = f[i][0] 最后一步是退一层跳两层的情况下爬到第i+1层的方法数目
等于 从第i层情况a的数目跳两层退一层。
这里不能考虑第i层的情况b的方法数,由于第i层情况b的数目是从第i+1层退一步得到的。
f[i+2][0] = f[i][0]+f[i][1] 最后一步是退一层跳两层的情况下爬到第i+2层的方法数目
等于 第i层全部情况跳两层。
3. Intialize:
f[0][0]=1初始化最開始没有爬的方法数目为1.
4. Answer:
f[n][0]+f[n][1] 爬到第n层a、b两种不同的方法的总和
Climbing Stairs爬楼梯——动态规划的更多相关文章
- climbing stairs(爬楼梯)(动态规划)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] 70. Climbing Stairs 爬楼梯问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] 70. Climbing Stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- [LeetCode] 746. Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)
题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...
- LeetCode 70. Climbing Stairs爬楼梯 (C++)
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- [Leetcode] climbing stairs 爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [leetcode]70. Climbing Stairs爬楼梯
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
随机推荐
- MySQL 数据库 分页查询
在使用MySQL 进行数据库分页查询的时候最主要是使用LIMIT子句进行查询: 首先来看一下LIMIT: LIMIT子句可以用来限制由SELECT语句返回过来的数据数量,它有一个或两个参数,如果给出两 ...
- Creating the Help Page in ASP.NET Web API
Introduction In this article we will define the process of creating the help page in the ASP .NET We ...
- Linux下nagios网络监控与/proc/net/tcp文件详解
问题描述:nagios自带的check_antp太过简约,除了状态统计输出外,什么参数都不提供.在面对不同应用服务器时,报警就成了很大问题. 问题描述:nagios自带的check_antp太过简约, ...
- Spring Boot 2.0 Intellij Idea 中图文详解打包成可执行Jar
我们使用Spring Boot 2.0 创建好我们的项目后,我们一般需要打包,然后部署到服务器上. 打包步骤: 1. 选中项目,右键——> Open Module Settings. 2. 切换 ...
- angular学习笔记(二十)-表单验证
本篇主要介绍angular中的表单验证: 表单验证主要有以下一些内容: 1. required指令: 相当于html5的required属性,验证不能为空 2. ng-maxlength属性: 验证内 ...
- Lintcode记录
汇总贴 56. Two Sum[easy] 167. Add Two Numbers[easy] 53. Reverse Words in a String[easy] 82. Single Numb ...
- python 中安装pandas
由于计算arima模型需要用到pandas,费尽千辛万苦找到了一个下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/,在这里能下载到很多我们要用的模块.找到 ...
- 山寨一个std::bind\boost::bind
这里是最初始的版本,参考https://github.com/cplusplus-study/fork_stl/blob/master/include/bind.hpp 提供了最简洁的实现方式. 第一 ...
- org.eclipse.jdt.internal.compiler包下的类找不到
到maven库上下载jar包:org.eclipse.jdt.core-3.13.jar <!-- https://mvnrepository.com/artifact/org.eclipse. ...
- 基于CSS3图片悬停放大特效
今天我们要来分享一款很酷的CSS3图片特效,这款图片特效可以利用鼠标滑过图片使其悬停放大,并使图片的周围出现发光的效果.配合黑色的背景,这款CSS3图片悬停放大效果显得更加立体大气,非常适合产品图片的 ...