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 ...
随机推荐
- 由于CentOS的系统安装了epel-release-latest-7.noarch.rpm 导致在使用yum命令时出现Error: xz compression not available问题
由于CentOS6的系统安装了epel-release-latest-7.noarch.rpm 导致在使用yum命令时出现Error: xz compression not available问题.解 ...
- unity 在Game视图中显示Gizmos
自己画的Gizmos要想在Game视图中能看到,需要把Game视图窗口右上角的"Gizmos"按钮点下去.如图: 比如,下面代码以角色的capsuleCollider中心为中心画一 ...
- php长链接
php 连接 mysql 是分为两步走的第一步:建立 php 到 mysql 服务器的 tcp/ip 通道 物理通道第二步:登录 mysql 服务器,建立到数据库的通道 逻辑通道 无论是长连接还是短连 ...
- for循环中的break与continue
break: 跳出循环,执行for循环下面的语句.continue: 跳出本次循环,执行下次循环.
- google protocol buffer的原理和使用(三)
介绍下怎么反序列化GoogleBuffer数据.并在最后提供本系列文章中所用到的代码整理供下载. 上一篇文章介绍了如何将数据序列化到了addressbook.data中.那么对于接受方而言该怎么解析出 ...
- 向Oracle数据库插入一条数据
这几天搞了一下Oracle数据库.可能用sql server习惯了,感觉好不方便.PL的界面友好度比sql server差远了 .既然都收购了PL了 为什么不给它做好一点呢?各种不便.郁闷 向Orac ...
- python random模块(获取随机数)的常用方法及示例
random.randomrandom.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniformrandom.uniform(a, b),用 ...
- 分享一个上传图片,图片压缩Unsupported Image Type解决方案
http://blog.csdn.net/frankcheng5143/article/details/53185201 *************************************** ...
- weblogic连接池问题总结(转载)
转自:某局Weblogic 连接池问题(现场报告)(Connection has been administratively disabled. Try later.) 目录 1. 概述 3 1.1 ...
- 用Python写一个猜数字游戏
2015.5.25第一天下载Python IDLE,写个猜数字的小游戏来熟悉这门语言: times=6 letters=[100] for i in range(1,times): a = input ...