算法D38| 动态规划1 | 509. 斐波那契数 70. 爬楼梯 746. 使用最小花费爬楼梯

理论基础

无论大家之前对动态规划学到什么程度,一定要先看 我讲的 动态规划理论基础。

如果没做过动态规划的题目,看我讲的理论基础,会有感觉 是不是简单题想复杂了?

其实并没有,我讲的理论基础内容,在动规章节所有题目都有运用,所以很重要!

如果做过动态规划题目的录友,看我的理论基础 就会感同身受了。

代码随想录

视频:从此再也不怕动态规划了,动态规划解题方法论大曝光 !| 理论基础 |力扣刷题总结| 动态规划入门_哔哩哔哩_bilibili

509. 斐波那契数

很简单的动规入门题,但简单题使用来掌握方法论的,还是要有动规五部曲来分析。

代码随想录

视频:手把手带你入门动态规划 | LeetCode:509.斐波那契数_哔哩哔哩_bilibili

Python:

太经典了。

python 复制代码
class Solution:
    def fib(self, n: int) -> int:
        a = 0
        b = 1
        for _ in range(n):
            a, b = b, a+b
        return a

C++:

cpp没有python同时赋值的操作,注意一下语法实现。

python 复制代码
class Solution {
public:
    int fib(int n) {
        int a = 0;
        int b = 1;
        int tmp;
        for (int i=0; i<n; i++) {
            tmp = b;
            b = a+b;
            a = tmp;         
        }
        return a;
    }
};

70. 爬楼梯

本题大家先自己想一想, 之后会发现,和 斐波那契数 有点关系。

代码随想录

视频:带你学透动态规划-爬楼梯(对应力扣70.爬楼梯)| 动态规划经典入门题目_哔哩哔哩_bilibili

Python:

和斐波那契思路基本一致,递归是会超时的,注意内存和时间的优化,O(n)最优。

python 复制代码
class Solution:
    def climbStairs(self, n: int) -> int:
        if n<=2: return n
        a, b = 1, 2
        for _ in range(2, n+1):
            a, b = b, a+b
        return a

C++:

return b可以保证在n=45时不溢出,return a在n=45时会溢出。

cpp 复制代码
class Solution {
public:
    int climbStairs(int n) {
        if (n<=2) return n;
        int a = 1;
        int b = 2;
        for (int i=2; i<n; i++) {
            int tmp = a+b;
            a = b;
            b = tmp;
        }
        return b;
    }
};

746. 使用最小花费爬楼梯

这道题目力扣改了题目描述了,现在的题目描述清晰很多,相当于明确说 第一步是不用花费的。

更改题目描述之后,相当于是 文章中 「拓展」的解法

代码随想录

视频讲解:动态规划开更了!| LeetCode:746. 使用最小花费爬楼梯_哔哩哔哩_bilibili

Python:

python 复制代码
class Solution:
    def minCostClimbingStairs(self, cost: List[int]) -> int:
        cost.append(0)
        a = b = 0
        for c in cost:
            if a>b:
                a, b = b, b+c
            else:
                a, b = b, a+c
        return b

C++:

cpp 复制代码
class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int a = 0;
        int b = 0;
        int tmp;
        cost.push_back(0);
        for (int c:cost) {
            tmp = b;
            if (a>b) {
                b += c;
            } else {
                b = a+c;
            }
            a = tmp;
        }
        return b;
    }
};
相关推荐
2601_962078192 小时前
Python中calendar.weekday用法
python·编程技巧·calendar·日期处理·weekday
2601_962218612 小时前
万象生鲜系统业财一体化底层打通技术自动生成经营账单
大数据·数据库·人工智能·python·算法
2601_966949652 小时前
为什么量化策略需要大量历史股票数据?从回测可信度理解数据规模
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
2601_962885722 小时前
如何用 Python 扫描 A 股跳空缺口并统计缺口回补概率?
java·前端·python
anew___2 小时前
蜂鸣器播放音乐——让 Arduino 唱出旋律
c++·stm32·单片机·嵌入式硬件·c
吞下星星的少年·-·2 小时前
The 2026 ICPC Asia East Continent Online Contest (II)(构造)
数据结构·算法
李高钢3 小时前
Python FastAPI 框架入门:从零搭建你的第一个高性能 API 服务
数据库·python·fastapi
ocean21033 小时前
2025-2026年Python面试高频知识点洞察
开发语言·python·面试·python八股文
Warson_L4 小时前
Python的OrderedDict
python
stolentime4 小时前
洛谷P10515 转圈题解
c++·算法·数学建模·贪心算法