[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  : [email protected]
 */

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  : [email protected]
 */

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;
    }
};
时间: 2024-10-25 23:16:12

[LeetCode] Climbing Stairs (Sequence DP)的相关文章

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

翻译 你正在爬一个楼梯. 它须要n步才干究竟顶部. 每次你能够爬1步或者2两步. 那么你有多少种不同的方法爬到顶部呢? 原文 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? 分析 动态规划基础题,首先设置3个变量用于

leetCode 70.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 1 or 2 steps. In how many distinct ways can you climb to the top? 思路:题目也比较简单,类似斐波那契. 代码如下: public class Solution { public int climbSta

leetcode_70题——Climbing Stairs(简单DP题)

Climbing Stairs Total Accepted: 54579 Total Submissions: 158996My Submissions Question Solution 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

LeetCode Unique Paths (简单DP)

题意: 给出一个m*n的矩阵,robot要从[1][1]走到[m][n],每次只能往下/右走,问有多少种走法? 思路: DP的经典问题.先将[1][1]设为1,然后两种走法就是分别用[i][j]去更新[i+1][j]和[i][j+1]. 观察一下数组的更新次序就可以知道,这很像完全背包的更新方式,那么就可以用一维数组就行了.更新方式从左到右就行了. 由于这是DP题,就没有必要去研究数学公式了(涉及等差数列求和). 1 class Solution { 2 public: 3 int unique

[LeetCode] Triangle(&#39;Bottom-up&#39; DP)

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

[LeetCode] Climbing Stairs [24]

题目 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? 原题链接(点我) 解题思路 爬楼梯:一次可以爬1阶或者2阶,问爬n阶楼梯有多少方法? 这是个典型的斐波拉切应用场景,我们下面来分析下: 对于1阶,只有 1 种方法

LeetCode: Climbing Stairs [070]

[题目] 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? [题意] 有个梯子有n阶,每次只能爬1阶或者2阶,为爬到梯子顶共有多少种爬法 [思路] 依次确定跳到每一阶上的爬法数目 这其实是一个斐波那契数列数列.假设A[

[leetcode]Climbing Stairs @ Python

原题地址: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? 解题思路: 爬楼梯问题.经典的动态规划问题.每次上

【LeetCode-面试算法经典-Java实现】【070-Climbing Stairs(爬楼梯)】

[070-Climbing Stairs(爬楼梯)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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? 题目大意 你正在爬一个楼梯,要走n步才