当前位置:网站首页>leetcode专项 动态规划入门
leetcode专项 动态规划入门
2022-07-16 11:18:00 【嗯我想想】
刷一下leetcode的动态规划专项,本篇博客为入门专项 传送门
第一天
509. 斐波那契数
AC代码:
class Solution {
public:
int fib(int n) {
if(n < 2) return n;
if(n == 2) return 1;
int a = 1, b = 1;
int c;
for(int i = 2; i < n; i++) {
c = a;
a = b;
b = c + a;
}
return b;
}
};
1137. 第 N 个泰波那契数
AC代码:
class Solution {
public:
int tribonacci(int n) {
if(n < 2) return n;
if(n == 2) return 1;
int a = 0, b = 1, c = 1;
int d;
for(int i = 2; i < n; i++) {
d = c;
c = a + b + c;
a = b;
b = d;
}
return c;
}
};
第二天
70. 爬楼梯
这个递推公式和斐波那契有点像。。。
AC代码:
class Solution {
public:
int climbStairs(int n) {
if(n <= 3) return n;
int a = 2, b = 3;
int c;
for(int i = 4; i <= n; i++) {
c = b;
b = a + b;
a = c;
}
return b;
}
};
746. 使用最小花费爬楼梯
最小花费问题,写出状态转移方程即可
AC代码:
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
int h = cost.size();
int w[1010];
memset(w, 0, sizeof(w));
for(int i = 2; i <= h; i++)
w[i] = min(cost[i - 2] + w[i - 2], cost[i - 1] + w[i - 1]);
return w[h];
}
};
第三天
198. 打家劫舍

AC代码:
class Solution {
public:
int rob(vector<int>& nums) {
int m = nums.size();
int f[110], g[110];
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
f[0] = nums[0];
for(int i = 1; i < m; i++) {
f[i] = g[i - 1] + nums[i];
g[i] = max(f[i - 1], g[i - 1]);
}
return max(f[m - 1], g[m - 1]);
}
};
213. 打家劫舍 II
边栏推荐
猜你喜欢

Scala 基础 (二):变量和数据类型

我发现了一款高效管理接口文档的神器

I found an artifact that can efficiently manage interface documents

Web APIs DOM event flow

面试官:Redis主从集群切换数据丢失问题如何应对?

自建个性化自动报价系统,应对多变报价模式

终于入选专业领域内容排行榜第一了,这段时间没有白累,感谢CSDN官方平台,幸苦了。

Today in history: the Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
![[deep learning] 40000 word notes! Yann Lecun deep learning open class 125 pages of dry goods are here](/img/03/0a7327191447926146e4b3abc66873.png)
[deep learning] 40000 word notes! Yann Lecun deep learning open class 125 pages of dry goods are here

越来越难,不行就得多听几次
随机推荐
It's getting more and more difficult. If you can't, you have to listen more times
[machine learning] logic regression principle and code
无心剑中译扎加耶夫斯基《试着赞美这残缺的世界》
从IT视角审视企业经营,B2B行业CIO谈如何从“成本中心”转到“增长中心”?
What does software testing need to learn? What skills do test engineers with an annual salary of 30w+ need to master?
1.6 方法
【华为联机对战】下载运行华为官方Unity示例代码,提示鉴权失败并返回错误码100114
[HMS core], [FAQ], [Health Kit] encountered some small problems in the process of integrating sports health services. Today, I share with you (Huawei watch, Bracelet + sports health service problems C
交换
元宇宙、NFT数字藏品是什么?
Web APIs DOM event flow
华为应用强制更新中,偶现点击“退出应用”退不出应用
华为云 X 黑湖丨一个系统,搞定制造业各种生产不畅!
[deep learning] 40000 word notes! Yann Lecun deep learning open class 125 pages of dry goods are here
内行,阿里大咖离职带出内部“高并发系统设计”学习手册
Analysis and summary of three technical solutions to realize app automation
[quick application] quick application user agreement and privacy policy content can jump many times. Click back and fail to return to the previous page. How to deal with it?
1.4 process control statement
UE4 blueprint learning chapter (6) -- branch, switch, filpflop, sequence
Pycharm crawler tutorial (only for technical exchange)