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)的更多相关文章

  1. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  2. LeetCode算法题-Min Cost Climbing Stairs(Java实现)

    这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...

  3. LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)

    翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 You are climbing a stair case. It tak ...

  4. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

  5. LeetCode算法题-Climbing Stairs(Java实现)

    这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...

  6. LeetCode Unique Paths (简单DP)

    题意: 给出一个m*n的矩阵,robot要从[1][1]走到[m][n],每次只能往下/右走,问有多少种走法? 思路: DP的经典问题.先将[1][1]设为1,然后两种走法就是分别用[i][j]去更新 ...

  7. 力扣—climbing stairs(爬楼梯) python实现

    题目描述: 中文: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 英文: You are cl ...

  8. 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 ...

  9. Bomb HDU - 3555 (数位DP)

    Bomb HDU - 3555 (数位DP) The counter-terrorists found a time bomb in the dust. But this time the terro ...

随机推荐

  1. Xcrysden-2

    XCrySDen -- (X-Window) CRYstalline Structures and DENsities Introduction to use. XCrySDen is a cryst ...

  2. vs code 插件推荐

    通用插件 HTML Snippets 超级实用且初级的 H5代码片段以及提示 HTML CSS Support 让 html 标签上写class 智能提示当前项目所支持的样式新版已经支持scss文件检 ...

  3. cdoj841-休生伤杜景死惊开 (逆序数变形)【线段树 树状数组】

    http://acm.uestc.edu.cn/#/problem/show/841 休生伤杜景死惊开 Time Limit: 3000/1000MS (Java/Others)     Memory ...

  4. [poj1269]Intersecting Lines

    题目大意:求两条直线的交点坐标. 解题关键:叉积的运用. 证明: 直线的一般方程为$F(x) = ax + by + c = 0$.既然我们已经知道直线的两个点,假设为$(x_0,y_0), (x_1 ...

  5. Web Components 规范学习

    最新的规范在这里:http://w3c.github.io/webcomponents/explainer/ 依据规范,有以下四个组成部分: Templates Custom Elements Sha ...

  6. 4-计算九位数以内各个位数字和为s的种类

    /*假设我们当前要求的是100以内各位数之和和等于s的数有多少个,可以想到就是10以内各位数之和等于s的数的个数再加上10到100内的各位数之和等于s的个数.令dp[i][j]就代表10的j次方内各位 ...

  7. 文件Move操作

    #coding=utf-8 import os import shutil stra = "G:/should/v3/a" strb = "G:/should/v3/b& ...

  8. ROS Learning-008 beginner_Tutorials ROS话题

    ROS Indigo beginner_Tutorials-07 ROS话题 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LT ...

  9. Linq select 语法

    文档:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b 1.可以对查询出来的结果做一些转换,下面的例子在数组中查找以"B&q ...

  10. Codeforces Recycling Bottles 模拟

    C. Recycling Bottles time limit per test: 2 seconds memory limit per test: 256 megabytes input: stan ...