当前位置:网站首页>五月集训(第28天)——动态规划
五月集训(第28天)——动态规划
2022-06-12 06:27:00 【误入佬群】
文章目录
前言
今天算法的内容是:动态规划
一、 爬楼梯
假设你正在爬楼梯。需要 n阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
示例 1:
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
示例 2:
输入:nums = [1]
输出:1
示例 3:
输入:nums = [5,4,-1,7,8]
输出:23
一、思路:
(1)如果是三阶以下直接返回即可
(2)如果是三阶以上则将前两次的状态相加就是当前的状态,循环n次;
二、源码
class Solution {
public:
int climbStairs(int n) {
int a=1,b=2,c=0;
if(n<3) return n;
for(int i=3 ;i<=n ;i++){
c=a+b;
a=b;
b=c;
}
return c;
}
};
三.知识点
斐波那契数列
二、 最大子数组和
给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
子数组 是数组中的一个连续部分。
示例 1:
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
示例 2:
输入:nums = [1]
输出:1
示例 3:
输入:nums = [5,4,-1,7,8]
输出:23
一、思路:
(1)过去的状态与下一次的状态相加并与下一次状态的进行对比,对比出现在最好的状态
(2)将过去所记录的最好的状态与现在的状态进行比较,选出最好的状态
(3)突然想到为什么 状态会 时刻变化呢?那是因为状态会随着循环次数的变化而与下一次状态相加变化着,可能会变弱,也可能会变强
二、源码
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max=-1000000,now=0;
for(int i=0;i<nums.size();i++){
now=now+nums[i] > nums[i]?now+nums[i]:nums[i];
max=max > now?max:now;
}
return max;
}
};
//第一次 超时辣 代码
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sum=0,max=-100000;
int len=nums.size();
for(int i=0 ;i<len ;i++){
for(int j=i ;j<len ;j++){
sum+=nums[j];
if(sum > max){
max=sum;
}
}
sum=0;
}
return max;
}
};
三.知识点
动态规划
边栏推荐
- 相机图像质量概述
- JS variable scope
- PHP development environment construction and database addition, deletion, modification and query
- C2w model - language model
- LeetCode-1185. Day of the week
- Introduction to the method of diligently searching for the alliance procedure
- The first principle of thinking method
- Leetcode January 13 daily question 747 At least twice the maximum number of other numbers
- LeetCode-2034. Stock price fluctuation
- LeetCode-419. Battleship on deck
猜你喜欢

Opencv_ 100 questions_ Chapter V (21-25)

. Net core - pass Net core will Net to cross platform

Dlib face detection

What states do threads have?

Chartextcnn (Ag dataset - news topic classification)

About why GPU early-z reduces overdraw

Solution: content type 'application/x-www-form-urlencoded; charset=UTF-8‘ not supported

Jetson TX2 machine brushing jetpack4.2 (self test successful version)

Nodemon cannot load the file c:\users\administrator\appdata\roaming\npm\nodemon PS1, because script execution is prohibited in this system

2021 RoboCom 世界机器人开发者大赛-本科组(初赛)
随机推荐
leetcode 300. Longest increasing subsequence
SQL injection - blind injection
On the normalization of camera rotation interpolation
SQL 注入-盲注
Process when solving vagrant up_ builder. rb:43:in `join‘: incompatible character encodings: GBK and UTF-8
English grammar_ Adverb_ With or without ly, the meaning is different
Unity surface shader with template buffer
The vs 2019 community version Microsoft account cannot be logged in and activated offline
SQL injection based on error reporting
Summary of some problems in sensor bring up
Unity3d script captures a sub area from the screen and saves it as texture2d, which is used to save pictures and maps
What states do threads have?
PHP read / write cookie
SQL注入——联合查询union
Modifying theme styles in typora
2021 RoboCom 世界机器人开发者大赛-本科组(初赛)
Qt-- realize TCP communication
. Net core - pass Net core will Net to cross platform
leetcode 278. First wrong version
Word2Vec