当前位置:网站首页>Introduction to leetcode special dynamic planning
Introduction to leetcode special dynamic planning
2022-07-18 17:10:00 【Well, let me see】
Brush it. leetcode Dynamic planning project of , This blog is an introductory project Portal
leetcode special Introduction to dynamic planning
The first day
509. Fibonacci number
AC Code :
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. The first N One tibonacci number
AC Code :
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;
}
};
the second day
70. climb stairs
This recursive formula is a bit like Fibonacci ...
AC Code :
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. Use the minimum cost to climb the stairs
Minimum cost problem , Write the state transition equation
AC Code :
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];
}
};
On the third day
198. raid homes and plunder houses

AC Code :
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. raid homes and plunder houses II
边栏推荐
- Analysis and summary of three technical solutions to realize app automation
- How to use pycharm to make a simple Snake game
- 抢效率!上云规划时间缩短一半,这家企业的CTO做对了什么?
- 【学习记录】暂时取消Tpro的控制权(简易)
- No 996, no involution, LETV has a "fairy day"?
- 【AGC】增长服务3-App Linking示例
- Vscode 1.69 changes and concerns (three-way merge / terminal integration, etc.)
- 如何统计游戏中的数据
- Error in v-on handler: “ReferenceError: Toast is not defined“
- PCB芯片散热焊盘如何设计?
猜你喜欢
随机推荐
从物理转 AI 、战数据库,95后程序员的职业选择
电脑PC与S7-200SMART PLC不在同一网段,如何建立通信连接?
[take you to learn UVM by hand] ~ record all errors encountered
Grab efficiency! What has the CTO of this enterprise done right to shorten the cloud planning time by half?
记录一个温度曲线的View
Common body and enumeration type
Error yolov5 PT to onnx error
Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return
[machine learning] logic regression principle and code
国际NFT交易所排行榜前10名
我的创作纪念日
污水排放监控,环保数采仪助力城市黑臭水体治理
[HMS core] [wallet kit] [solution] why can't Huawei wallet's client sample code run
1.6 方法
10张图教你同步与异步(转载)
Building vector database from scratch: source code compilation and installation of Milvus (II)
【学习记录】暂时取消Tpro的控制权(简易)
LeetCode SQL 1669. 176. 第二高的薪水
【HarmonyOS】【ARKUI】鸿蒙 ets方式tabs+tabcontent 实现底部导航栏
如何利用pycharm制作一个简单的贪吃蛇小游戏








