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 ...
随机推荐
- phpexcel单元格内换行
我说的这个换行不是字多了,自动换行的那种,是在特定位置添加换行符 代码如下: $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A4 ...
- 安装composer slim(php web api micro services)
1. 安装php7 2. 下载 https://getcomposer.org/composer.phar 3. 开启ssh, 在 php.ini中.extension=C:\Program File ...
- CentOS的字符集locale的设置
LANGLC_*的默认值,是最低级别的设置,如果LC_*没有设置,则使用该值.类似于 LC_ALL. LC_ALL它是一个宏,如果该值设置了,则该值会覆盖所有LC_*的设置值.注意,LANG的值不受该 ...
- js检查页面上有无重复id的代码分享
用js代码检查一个页面上是否用重复的id. 方法一: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ...
- NY891 区间选点 找点
找点 时间限制:2000 ms | 内存限制:65535 KB 难度:2 描述 上数学课时,老师给了LYH一些闭区间,让他取尽量少的点,使得每个闭区间内至少有一个点.但是这几天LYH太忙了,你们帮 ...
- 如何保证RabbitMQ的消息不丢失及其背后的原理
一.消息为什么丢失 RabbitMQ默认情况下的交换机和队列以及消息是非持久化的,也就是说在服务器重启或者宕机恢复后,之前创建的交换机和队列都将不复存在,之前未消费的消息也就消失不见了.原因在于每个队 ...
- java.util.logging.Logger日志生成过程浅析 (转)
http://www.tuicool.com/articles/vy6Zrye ****************************************** java.util.logging ...
- hadoop输出lzo文件并添加索引
public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); ...
- HTML源文件编码的问题
刚才使用sublime text编辑html文件,在html中使用meta tag指定了charset,如下 <meta http-equiv="content-type" ...
- 纯绿色 jsonUtil工具
package com.daditech.common.util; import java.lang.reflect.Field; import java.lang.reflect.Method; i ...