[LeetCode] Climbing Stairs (Sequence DP)
Climbing Stairs
https://oj.leetcode.com/problems/climbing-stairs/
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
这题比较简单,可以使用动态规划来求解。请看以下分析:
State:f[i] 表示从起点出发达到第 i 个位置的方案总数
Function:由于第 i 个位置可以由第 i – 2 个位置走两步或者由第 i – 1 个位置走一步而到达,因此有以下状态转移方程:
f[i] = f[i-1] + f[i-2]
Initialize:1. 从起点走到第一个位置,显然只有走 1 步到达这一种方案。
2. 从起点走到第二个位置,有两种方案:直接走 2 步或者每次走 1 步,走 2 次。因此,初始化状态如下:
f[0] = 1
f[1] = 2
注意:数组下标从0开始。
Answer:f[n - 1] 。
下面为 AC 的代码:
/**
* Author : Zhou J
* Email : zhoujx0219@163.com
*/ class Solution {
public:
int climbStairs(int n)
{
if (n == 0)
{
return 0;
} // State: 从起点走到第 i 个位置的方案总数
int sum[n]; // initialize
sum[0] = 1;
if (n >= 2)
{
sum[1] = 2;
} // switch the state
for (size_t ix = 2; ix < n; ++ix)
{
sum[ix] = sum[ix - 1] + sum[ix - 2];
} return sum[n - 1];
}
};
Optimize
当然,此处并不需要使用一个 n 维的数组来存放 State ,观察状态转移方程就可以知道,此处只需要两个变量来存放状态即可。因此下面的代码对空间做了进一步的优化:
/**
* Author : Zhou J
* Email : zhoujx0219@163.com
*/ class Solution {
public:
int climbStairs(int n)
{
if (n <= 2)
{
return n;
} size_t now;
size_t lastlast = 2; // f[1]
size_t last = 1; // f[0] // switch the state
for (size_t ix = 2; ix < n; ++ix)
{
now = lastlast + last;
last = lastlast;
lastlast = now;
} return now;
}
};
[LeetCode] Climbing Stairs (Sequence DP)的更多相关文章
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- LeetCode算法题-Min Cost Climbing Stairs(Java实现)
这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...
- LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- LeetCode算法题-Climbing Stairs(Java实现)
这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...
- LeetCode Unique Paths (简单DP)
题意: 给出一个m*n的矩阵,robot要从[1][1]走到[m][n],每次只能往下/右走,问有多少种走法? 思路: DP的经典问题.先将[1][1]设为1,然后两种走法就是分别用[i][j]去更新 ...
- 力扣—climbing stairs(爬楼梯) python实现
题目描述: 中文: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 英文: You are cl ...
- leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...
- Bomb HDU - 3555 (数位DP)
Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...
随机推荐
- Path-O-LOGIC Keynote
[Path-O-LOGIC Keynote] 1. OnSpawned()OnSpawned(SpawnPool pool) 2. OnDespawned()OnDespawned(SpawnPool ...
- Css定位元素
Css定位selenium极力推荐使用Css定位,而不是xpath定位元素,原因是css定位比xpath定位块,速度快,语法更加简洁 css常用的定位方法:1.find_element_by_css_ ...
- django1.8模板位置的设置setting.py
大多数django教程比较老,给出的template的设置方案为: 更改工程下的setting.py文件, TEMPLATE_DIRS = ( os.path.join( APP_DIR, ' ...
- overflow: scroll
overflow: scroll在安卓5.0的情况下,不论内容是否填满屏幕,都会强制解析出滚动条,所以最好是使用overflow: auto
- ROI
1.ROI简介 ROI(region of interest),感兴趣区域.机器视觉.图像处理中,从被处理的图像以方框.圆.椭圆. 不规则多边形等方式勾勒出需要处理的区域,称为感兴趣区域,ROI.在H ...
- python作业之生成随机数
作业要求 生成一个6个字符长度的随机数,要求是包括字母和数字的组合 import random l1 = [] for i in range(6): a = random.randrange(0,10 ...
- .net为图片添加水印(转) jpg png和gif格式
.net为图片添加水印(转) jpg png和gif格式 .net为图片添加水印(转) jpg png和gif格式,转自csdn的hyde82,现在跟大家一起来分享下: 利 用.net中System. ...
- Volley下载图片存放在data/data下 networkImageView lrucache
networkImageView 设置图片的方法 (有效) imageView.setImageUrl("https://www.baidu.com/img/bd_logo1.png&qu ...
- vue2.0 tab切换几种方式
第一种 比较灵活简单的方式(切换改变部分的内容在组件中比较方便操作) <template> <div id="app"> <ul> <li ...
- linux下memcache安装
安装配置 1. 安装libevent # tar zxf libevent-1.4.6-stable.tar.gz # cd libevent-1.4.6-stable # ./configure # ...