当前位置:网站首页>【Day_05 0422】连续最大和
【Day_05 0422】连续最大和
2022-07-26 06:08:00 【安河桥畔】
连续最大和
题目来源
牛客网:连续最大和
题目描述
一个数组有 N 个元素,求连续子数组的最大和。 例如:[-1,2,1],和最大的连续子数组为[2,1],其和为 3
输入描述
输入为两行。 第一行一个整数n(1 <= n <= 100000),表示一共有n个元素 第二行为n个数,即每个元素,每个整数都在32位int范围内。以空格分隔。.
输出描述
所有连续子数组中和最大的值。
示例1
输入
3
-1 2 1输出
3
思路分析
- 数组v保存数据,定义一个sum保存临时的最大值,max保存比较过后的最大值
- 遍历数组,数组元素和用sum表示,如果之前的sum值小于0,则可以被舍弃,用当前v[i]替代其值,因为要求的是最大连续和,如果sum值大于0,则保留之前的sum,并且需要加上当前的v[i]
- 每次sum的值变化,都要与max进行比较,两者之间较大的就是遍历到当前位置的连续最大和
- 状态方程式: max( dp[ i ] ) = getMax( max( dp[ i -1 ] ) + arr[ i ] ,arr[ i ] ),这个方程表示的含义是,以下标为 i 结尾的数组的最大序列和,即每一个位置为结尾,之前的最大序列和都是该位置前一个位置的最大序列和加上当前位置的元素的值,与当前位置元素的值比较,两者之前较大的。而当然,如果前一个位置的最大序列和小于0,则最大序列和是当前位置元素的值。
代码展示
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
int n = 0;
while (cin >> n)
{
//vector添加元素,先设置容器大小
v.resize(n);
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
int sum = v[0];
int max = v[0];
for (int i = 0; i < v.size(); i++)
{
if (sum < 0)
{
//sum的值若小于0,则v[i]之前的元素被舍弃
sum = v[i];
}
else
{
//如果sum值大于0,为了得到之前大于0的部分,无论正负
//都要加上当前v[i]的值
sum += v[i];
}
if (sum > max)
{
max = sum;
}
}
cout << max;
}
return 0;
}
总结
- vector类对象添加元素,先使用v.resize()设置容器大小
边栏推荐
- Intelligent fire protection application based on fire GIS system
- 2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
- Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!
- Easycvr video square channel display and video access full screen display style problem repair
- Kingbasees SQL language reference manual of Jincang database (11. SQL statement: abort to alter index)
- Leetcode:741. picking cherries
- Convolutional neural network (III) - target detection
- Solution to slow download speed of vagrant
- ETCD数据库源码分析——Cluster membership changes日志
- 分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
猜你喜欢

Embedded sharing collection 15

Kingbasees SQL language reference manual of Jincang database (6. Expression)

Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))

Etcd database source code analysis - cluster membership changes log

CV (1)- Introduction

Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib

Matlab 向量与矩阵

Recursive processing - subproblem

Distributed | practice: smoothly migrate business from MYCAT to dble

Intelligent fire protection application based on fire GIS system
随机推荐
VRRP principle and basic commands
对接微信支付(二)统一下单API
Implementation of PHP multitask second timer
英语句式参考纯享版 - 状语从句
Mysql45 speak in simple terms index
Latex同时合并表格的多行多列
Optical quantum milestone: 3854 variable problems solved in 6 minutes
Day110.尚医通:Gateway集成、医院排班管理:科室列表、根据日期统计数据、排班详情
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
PHP 多任务秒级定时器的实现方法
光量子里程碑:6分钟内解决3854个变量问题
日志收集分析平台搭建-1-环境准备
Amd zen4 game God u reached 208mb cache within this year, which is unprecedented
Kingbasees SQL language reference manual of Jincang database (9. Common DDL clauses)
Ros2 knowledge: DDS basic knowledge
WebAPI整理
JDBC streaming query and cursor query
Mysql45 talking about logging system: how does an SQL UPDATE statement execute?
Interview difficulties: difficulties in implementing distributed session, this is enough!
Can you make a JS to get the verification code?