当前位置:网站首页>Leetcode53. maximum subarray and
Leetcode53. maximum subarray and
2022-07-19 05:13:00 【Doraemon 0219】
Title Description
Give you an array of integers nums , Please find a continuous subarray with the largest sum ( A subarray contains at least one element ), Return to its maximum and .
Subarray Is a continuous part of the array .
Example 1:
Input :nums = [-2,1,-3,4,-1,2,1,-5,4]
Output :6
explain : Continuous subarray [4,-1,2,1] And the biggest , by 6 .
Example 2:
Input :nums = [1]
Output :1
Example 3:
Input :nums = [5,4,-1,7,8]
Output :23
( source : Power button (LeetCode) link :https://leetcode.cn/problems/maximum-subarray)
The difficulty of this problem lies in , It is not simply a problem of finding the maximum sum , But for Successive The largest sum of subarrays . The first idea is to assume that the maximum and initial are nums[0], From the second number num[1] Start traversing the entire array , If num[0]>0, Update num[1] The value of is num[1]+num[0], If num[0]<0, Do not update the array , Enter the next cycle , If num[1]>0, Update num[2] The value of is num[2]+num[1]……maxSum Is the present maxSum and num[i] The maximum of both .
The code is as follows :
class Solution
{
public:
int maxSubArray(vector<int>& nums)
{
int maxSum = nums[0];
for(int i = 1; i < nums.size(); i++)
{
if(nums[i-1] > 0)
{
nums[i] += nums[i-1];
}
maxSum = max(maxSum, nums[i]);
}
return maxSum;
}
};The time complexity is O(n).
There is another solution to this problem ( Violence solution ), It's a double-layer cycle, each update max Value , This solution is easy to think of , But it may exceed the time limit :
class Solution
{
public:
int maxSubArray(vector<int> &nums)
{
// Similar to the problem of finding the maximum and minimum value , The initial value must be defined as the theoretical minimum and maximum value
int max = 0;
int numsSize = int(nums.size());
for (int i = 0; i < numsSize; i++)
{
int sum = 0;
for (int j = i; j < numsSize; j++)
{
sum += nums[j];
if (sum > max)
{
max = sum;
}
}
}
return max;
}
};The starting point of outer cycle control is i, The inner loop starts from i Start continuous summation , Find continuous and sum The maximum of , The double-layer loop result will find the maximum value of the sum of the continuous sub arrays of the entire array max.
The time complexity is O(n^2).
边栏推荐
- IDL 读取葵花8(Himawari-8)HSD数据
- Convolutional neural network
- es6新增-数组部分
- 机器学习之PCA特征降维+案例实践
- 哨兵二号轨道数据下载
- User login - and create SMS verification code
- [batch] batch delete intermediate folder - personal research script
- Using JS to realize the second level menu of anjuke and the full version (demonstration of precautions and problem points)
- es6新增-let和const (var的缺点&let及const)
- mysql数据库实验实训6,数据视图(详细)
猜你喜欢

es6新增-Symbol数据类型

游玩数据获取与数据分析、数据挖掘 【2022.5.30】

决策树原理和案例应用-泰坦尼克号生存预测
![[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)](/img/e6/beea0bb0a9f4b41206c6fcb130fdfd.png)
[2022 10th Teddy Cup Challenge] Title A: complete version of pest identification (general idea. Detailed process and code and results CSV in compressed package)

【2022第十届‘泰迪杯’挑战赛】A题:害虫识别完整版(大致思路。详细过程和代码以及结果csv在压缩包中)

模拟库函数

这么6的刷题网站你不会没听说过吧?你已经out 了?

哨兵二号轨道数据下载

Pygame:外星人入侵

uniapp中使用ucharts图表,饼状图,柱状图,折线图
随机推荐
【C语言—零基础第十四课】变量的作用域与存储类
STL容器——vector的基本操作
es6新增-Symbol数据类型
User management - restrictions
模拟库函数
小程序editor富文本编辑使用及rich-text解析富文本
02_ Movie recommendation (contentbased)_ User portrait
Interface parameters return encapsulated class result
一个问题的探讨
获取URL参数的两种方法及location对象的各项获取方式
mysql数据库实验实训5,数据查询yggl数据库查询(详细)
【C语言—零基础第八课】循环结构与break continue
多功能(实现)封装函数
Travel data acquisition, data analysis and data mining [2022.5.30]
决策树原理和案例应用-泰坦尼克号生存预测
【2022第十届‘泰迪杯’挑战赛】A题:害虫识别完整版(大致思路。详细过程和代码以及结果csv在压缩包中)
IDL调用6S大气校正
使用js中的(offset,page)实现登录效果
【C语言—零基础_学习_复习_第五课】基本运算符的运算性质
【C语言—零基础第七课】顺序结构与选择结构