LeetCode | 509. Fibonacci Number, 70. Climbing Stairs, 746. Min Cost Climbing Stairs

本文介绍了如何使用动态编程和递归方法解决LeetCode上的两个阶梯问题:计算Fibonacci数列和爬楼梯的不同方式,以及最小成本爬楼梯。详细展示了代码实现和思路分析。

509. Fibonacci Number

Link: https://leetcode.com/problems/fibonacci-number/

Description

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), for n > 1.

Given n, calculate F(n).

Approach

Dynamic Programming

  • If n equals to 0 or 1, return n.
  • Else, initialize a dp array to save the Fibonacci number of the current index. Initialize the the index 0 of the dp array to 0 and index 1 to 1.
  • Loop the array from index 2:
    • Add the sum of the previous index and the index before the pervious index to the current element.

Recursion

  • If n equals to 0 or 1, return n.
  • Else, return the sum of the previous index and the index before the pervious index to the current element.

Solution

// DP
class Solution {
    public int fib(int n) {
        if (n == 0 || n == 1)
            return n;
        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i <= n; i++)
            dp[i] = dp[i - 1] + dp[i - 2];
        return dp[n];
    }
}

// Recursion
class Solution {
    public int fib(int n) {
        if (n == 0 || n == 1)
            return n;
        return fib(n - 1) + fib(n - 2);
    }
}

70. Climbing Stairs

Link: https://leetcode.com/problems/climbing-stairs/

Description

You are climbing a staircase. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Approach

  • The dp array is used to store the distinct ways to climb to the top.
  • The first step has only one way and the second step has two ways so we initialize index 1 the dp array to 0 and 1 to 2.
  • Loop the dp array from index 2:
    • There are two ways to dlimb to the current index, from the previous step or the step before the previous step. The distinct ways to climb to the current index is the combination of the two ways.

Solution

class Solution {
    public int climbStairs(int n) {
        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;
        for (int i = 3; i <= n; i++)
            dp[i] = dp[i - 1] + dp[i - 2];
        return dp[n];
    }
}

746. Min Cost Climbing Stairs

Link: https://leetcode.com/problems/min-cost-climbing-stairs/

Description

You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps.

You can either start from the step with index 0, or the step with index 1.

Return the minimum cost to reach the top of the floor.

Appraoch

  • The meaning of the dp array is to store the cost to reach ith step. So we need to calculate the cost of the step after the final step.
  • The first step does not need cost so we initialize index 0 and 1 of the dp array to 0.
  • Loop through the dp array from index 2:
    • There are two ways to climb to the current index, the first one is climbing from the previous index, the second one is climbing from the index before the previous index. Compute the cost of the two ways and save the smaller one to the current index.

Solution

class Solution {
    public int minCostClimbingStairs(int[] cost) {
        int[] dp = new int[cost.length + 1];
        dp[0] = 0;
        dp[1] = 0;
        int cnt = 0;
        for (int i = 2; i < dp.length; i++) {
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }
        return dp[cost.length];
    }
}
<think>好的,我需要帮助用户查找LeetCode70题的解决方案和题目描述。首先,我应该确认用户的问题是关于LeetCode的爬楼梯问题,即Climbing Stairs。根据用户提供的引用信息,引用[1]、[3]和[5]提到了这个问题,尤其是引用[3]给出了一个具体的例子,输入3时输出3,说明有3种方法爬到顶部。 接下来,我需要整理题目描述。题目通常要求给定n阶楼梯,每次可以爬1或2步,返回不同的方法数。根据引用[3]的例子,当n=3时,答案是3,这符合斐波那契数列的规律,因为f(3)=f(2)+f(1)=2+1=3。 然后,解决方案部分。用户提供的引用[1]中提到了一种优化方法,将时间复杂度从$O(n^2)$降低到$O(n)$,可能是指动态规划的方法。动态规划是解决这个问题的常用方法,因为每一步的方法数等于前一步和前两步的方法数之和。此外,还可以使用递归或记忆化搜索,但动态规划在时间上更高效。 接下来,我需要检查是否有其他方法,比如矩阵快速幂或斐波那契公式,但根据用户提供的引用,可能只需要提到动态规划和递归两种方法。同时,引用[5]中提到的Python链表操作可能不相关,但需要注意Python的自动内存管理,这可能影响代码的写法,比如不需要手动删除节点。 然后,需要确保代码示例正确。动态规划的Python实现应该使用循环,从1到n逐步计算。递归方法可能会有栈溢出的问题,但作为示例可以提及。根据引用[1]的代码示例,用户可能希望看到简洁的代码,如使用变量交换来优化空间复杂度到$O(1)$。 最后,生成相关问题。用户可能对时间复杂度、空间复杂度优化、不同解法比较感兴趣,比如动态规划与递归的区别,或者如何用数学方法解决。同时,应用场景可能涉及斐波那契数列的其他应用。 需要确保回答结构清晰,分点说明题目描述、解决方案和代码示例,并正确引用用户提供的资料。特别是引用[1]和[3]应出现在适当的位置,比如动态规划的解释和示例部分。</think>### LeetCode Problem 70. Climbing Stairs 题解与题目描述 #### 题目描述 给定一个整数$n$表示楼梯阶数,每次可以爬1或2个台阶,求有多少种不同的方法可以到达楼顶[^3]。 示例: 当$n=3$时,共有3种方法: 1. 1步 + 1步 + 1步 2. 1步 + 2步 3. 2步 + 1步 --- #### 解决方案 该问题本质是斐波那契数列的变种,递推公式为: $$f(n) = f(n-1) + f(n-2)$$ 其中$f(1)=1$,$f(2)=2$。 ##### 方法1:动态规划(优化空间) 通过滚动变量优化空间复杂度至$O(1)$,时间复杂度$O(n)$: ```python def climbStairs(n: int) -> int: if n <= 2: return n a, b = 1, 2 for _ in range(3, n+1): a, b = b, a + b return b ``` ##### 方法2:递归(带记忆化) 通过缓存中间结果减少重复计算,时间复杂度$O(n)$: ```python from functools import lru_cache @lru_cache(maxsize=None) def climbStairs(n: int) -> int: if n <= 2: return n return climbStairs(n-1) + climbStairs(n-2) ``` --- #### 复杂度分析 | 方法 | 时间复杂度 | 空间复杂度 | 适用场景 | |------------|------------|------------|------------------------| | 动态规划 | $O(n)$ | $O(1)$ | 大规模输入(推荐) | | 递归 | $O(n)$ | $O(n)$ | 小规模输入或教学示例 | --- 相关问题
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值