当前位置:网站首页>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).
边栏推荐
- 【Es6】forEach,for...in ,for...of专栏,让你通过项目案例快速分辨各种for语句的使用方式及区别(完整版)内部有详细注释
- 关于New_Online_Judge_1081_哥德巴赫猜想的思考
- 【2022第十届‘泰迪杯’挑战赛】A题:害虫识别完整版(大致思路。详细过程和代码以及结果csv在压缩包中)
- Harmonyos fourth training notes
- Wechat applet status bar
- 【C语言_复习_学习第二课】什么是进制?进制之间应该如何转换
- Interface parameters return encapsulated class result
- IDL 6S查找表
- 02_ Movie recommendation (contentbased)_ User portrait
- H5如何获取内网IP和公网IP
猜你喜欢

实习项目3-更改所有者

机器学习之特征提取(类别特征进行数值化、离散化、文本特征进行数值化)

IText modify PDF Text

决策树原理和案例应用-泰坦尼克号生存预测

Elment UI usage

微信小程序云开发使用方法-1

Internship project 2 - Homepage configuration - my data module

Ucharts chart, pie chart, bar chart and line chart are used in uniapp

Applet editor rich text editing and rich text parsing

实习项目2-主页配置-我的数据模块
随机推荐
基于RTX30显卡的ArcGIS Pro2.8深度学习环境配置
【C语言—零基础_学习_复习_第四课】数据类型及其运算
PyGame installation -requirement already satisfied
MD5 password encryption
Uniapp uses uview to realize folding panel
学习C语言第二天
Two methods of rotation chart and automatic rotation
学习C语言的第五天
机器学习之特征提取(类别特征进行数值化、离散化、文本特征进行数值化)
【Es6】详细解说Set ,Array的常用对象及其他方法(完整版)
es6新增-数组/对象的解构赋值
【C语言—零基础第八课】循环结构与break continue
百度地图 实现 热力图
PAT乙级1002:写出这个数
热更新及其原理
Wechat applet status bar
ThreadLocal thread safety example and its principle
微信小程序状态栏
Mysql database experiment training 6, data view (detailed)
第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)