【力扣 - 爬楼梯】

题目描述

假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
在这里插入图片描述

方法一:动态规划

思路和算法

我们用 f(x) 表示爬到第 x 级台阶的方案数,考虑最后一步可能跨了一级台阶,也可能跨了两级台阶,所以我们可以列出如下式子:

f(x)=f(x−1)+f(x−2)

它意味着爬到第 x 级台阶的方案数是爬到第 x−1 级台阶的方案数和爬到第 x−2 级台阶的方案数的和。很好理解,因为每次只能爬 1 级或 2 级,所以 f(x)只能从 f(x−1)f(x−2) 转移过来,而这里要统计方案总数,我们就需要对这两项的贡献求和。

以上是动态规划的转移方程,下面我们来讨论边界条件。我们是从第 0 级开始爬的,所以从第 0 级爬到第 0 级我们可以看作只有一种方案,即 f(0)=1;从第 0 级到第 1 级也只有一种方案,即爬一级,f(1)=1。这两个作为边界条件就可以继续向后推导出第 n 级的正确结果。我们不妨写几项来验证一下,根据转移方程得到 f(2)=2f(3)=3f(4)=5,……,我们把这些情况都枚举出来,发现计算的结果是正确的。

我们不难通过转移方程和边界条件给出一个时间复杂度和空间复杂度都是 O(n) 的实现,但是由于这里的 f(x) 只和 f(x−1)f(x−2) 有关,所以我们可以用「滚动数组思想」把空间复杂度优化成 O(1)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码

int climbStairs(int n) {
    // Initialize three variables to keep track of the number of ways to climb stairs
    int p = 0, q = 0, r = 1;
    
    // Loop from 1 to n to calculate the number of ways to climb n stairs
    for (int i = 1; i <= n; ++i) {
        // Update p to the previous value of q
        p = q;
        // Update q to the previous value of r
        q = r;
        // Update r to the sum of p and q, representing the total ways to climb i stairs
        r = p + q;
    }
    
    // Return the total number of ways to climb n stairs
    return r;
}

复杂度分析

  • 时间复杂度:循环执行 n 次,每次花费常数的时间代价,故渐进时间复杂度为 O(n)
  • 空间复杂度:这里只用了常数个变量作为辅助空间,故渐进空间复杂度为 O(1)

方法二:矩阵快速幂

思路

以上的方法适用于 n 比较小的情况,在 n 变大之后,O(n) 的时间复杂度会让这个算法看起来有些捉襟见肘。我们可以用「矩阵快速幂」的方法来优化这个过程。
在这里插入图片描述
在这里插入图片描述

代码

// Define a struct to represent a 2x2 matrix
struct Matrix {
    long long mat[2][2];
};

// Function to multiply two matrices
struct Matrix multiply(struct Matrix a, struct Matrix b) {
    struct Matrix c;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            // Calculate the elements of the resulting matrix by multiplying and summing
            c.mat[i][j] = a.mat[i][0] * b.mat[0][j] + a.mat[i][1] * b.mat[1][j];
        }
    }
    return c;
}

// Function to calculate the n-th power of a matrix using binary exponentiation
struct Matrix matrixPow(struct Matrix a, int n) {
    struct Matrix ret;
    // Initialize the result matrix with identity values
    ret.mat[0][0] = ret.mat[1][1] = 1;
    ret.mat[0][1] = ret.mat[1][0] = 0;

    while (n > 0) {
        if ((n & 1) == 1) {
            // If the current power is odd, multiply the result by the base matrix
            ret = multiply(ret, a);
        }
        n >>= 1; // Right shift to divide the power by 2
        a = multiply(a, a); // Square the base matrix
    }
    return ret;
}

// Function to calculate the number of ways to climb n stairs
int climbStairs(int n) {
    struct Matrix ret;
    // Initialize the matrix with specific values for the climbing stairs problem
    ret.mat[1][1] = 0;
    ret.mat[0][0] = ret.mat[0][1] = ret.mat[1][0] = 1;

    // Calculate the n-th power of the matrix to get the result
    struct Matrix res = matrixPow(ret, n);
    return res.mat[0][0]; // Return the number of ways to climb n stairs
}

复杂度分析

  • 时间复杂度:同快速幂,O(log ⁡n)
  • 空间复杂度:O(1)

方法三:通项公式

思路

在这里插入图片描述

代码

// Function to calculate the number of ways to climb n stairs
int climbStairs(int n) {
    // Calculate the square root of 5
    double sqrt5 = sqrt(5);
    
    // Calculate the n-th Fibonacci number using Binet's formula
    double fibn = pow((1 + sqrt5) / 2, n + 1) - pow((1 - sqrt5) / 2, n + 1);
    
    // Divide the Fibonacci number by the square root of 5 and round it to the nearest integer
    return (int) round(fibn / sqrt5);
}

总结

在这里插入图片描述

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

六月悉茗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值