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. java并发:Semaphore

    Semaphore是常见的同步工具类之一. Semaphore翻译成字面意思为 信号量,Semaphore可以控制同时访问的线程个数,也称为permit(许可). 在构造函数中指定permits个数. ...

  2. input限制数字输入

    onkeyup="this.value=this.value.replace(/\D/g,'')"

  3. UVa 12100 Printer Queue(queue或者vector模拟队列)

    The only printer in the computer science students' union is experiencing an extremely heavy workload ...

  4. PythonQt在windows下的编译

    笔者最近在做Qt方面的开发工作,然后需要用到脚本程序对程序内部进行扩展,就很自然的想到了Python,度娘一下发现了一款神器,也就是今天给大家介绍的主角:PythonQt 今天首先给大家介绍下Pyth ...

  5. 品味性能之道<七>:索引基础

    一.索引概述      索引(index),它是数据库必不可少的一部分.它其实很简单呐!很好理解.      索引好比如一本书的目录,一张地图,一个写字楼里挂在大堂墙上的公司名录,一个地铁站的出口指示 ...

  6. 前后台交互(打开前端页面,不传递任何数据,发送ajax请求)

    1.打开前端,不传递任何数据 <script src="./jquery.min.js"></script> <script> $(docume ...

  7. laravel数据库操作

    一.配置文件路径:/.env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT= DB_DATABASE=test DB_USERNAME=root DB_P ...

  8. FEATURE_MCT_READERDIRECT问题

    刚才查了一下,好像与一个叫CCID的驱动有关.你把FEATURE_MCT_READEDIRECT定义成0x08,再make一下试试.

  9. 并发编程(四)TaskFuture

    并发编程(四)TaskFuture ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<O ...

  10. 递归生成treeview树形节点(没有用递归函数之后会有补充,这里只用系统的内置方法去生成)

    using System;using System.Collections.Generic;using System.ComponentModel;using System.IO;using Syst ...