当前位置:网站首页>Leetcode skimming -- bit by bit record 019
Leetcode skimming -- bit by bit record 019
2022-07-22 11:35:00 【Robust least squares support vector machine】
19. The finger of the sword Offer 42. The maximum sum of successive subarrays
requirement
Enter an integer array , One or more consecutive integers in an array form a subarray . Find the maximum sum of all subarrays .
The required time complexity is O(n).
Ideas
Dynamic programming :
State definition : Set up a dynamic programming list dp ,dp[i] Represented by elements nums[i] Is the maximum sum of consecutive subarrays at the end .
Transfer equation : if dp[i-1]≤0 , explain dp[i - 1] Yes dp[i] Produce negative contribution , namely dp[i-1] + nums[i] Not so good nums[i] Itself big . if dp[i-1]>0, explain dp[i - 1] Yes dp[i] Make a positive contribution .
The initial state : dp[0] = nums[0], That is to nums[0] The maximum sum of consecutive subarrays at the end is nums[0].
Return value : return dp The maximum value in the list , Represents the global maximum
Problem solving
#include <iostream>
using namespace std;
#include <vector>
class Solution {
public:
int maxSubArray(vector<int>& nums) {
vector<int> dp(nums.size(), 0);
int max = nums[0]; // Initialize the maximum subarray and , For the first element of the array
dp[0] = nums[0];
for (int i = 1; i < nums.size(); i++) {
if (dp[i - 1] <= 0)
dp[i] = nums[i]; // dp[i - 1] Less than or equal to 0, Negative contribution to subarray sum , So no need +
else
dp[i] = dp[i - 1] + nums[i]; // dp[i - 1] Greater than 0, Make a positive contribution to subarray and , So we need to +
if (dp[i] > max)
max = dp[i];
}
return max;
}
};
void test01()
{
Solution soulution;
vector<int> nums;
nums.push_back(-2);
nums.push_back(1);
nums.push_back(-3);
nums.push_back(4);
nums.push_back(-1);
nums.push_back(2);
nums.push_back(1);
nums.push_back(-5);
nums.push_back(4);
cout << soulution.maxSubArray(nums) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
I hope this article can help you , If there is anything wrong with the above , Welcome to correct
Sharing determines the height , Learn to widen the gap
边栏推荐
- 同花顺开通华泰证券账户安全吗?
- Paper sharing, target detection, image segmentation, supervised learning, etc
- MySQL Varchar前缀索引的一个细节
- 基于JSP+Servlet+MySQL的新闻博客发布系统
- Interview shock 67: talk about tcp/ip protocol? And the role of each layer?
- 标签平滑(LabelSmoothing)介绍与代码实现
- 基于SSM+MySQL+EasyUI+的博客论坛管理系统
- 图解BERT、ELMo等 | NLP迁移学习开端
- Using virtual to implement hooks in solidity
- 论文分享目标检测、图像分割、监督学习等
猜你喜欢
Redis的 缓存穿透、击穿、雪崩
利用Kaggle的API对数据集进行下载
软件测试工程师 | 不拼学历,还能进大厂吗?
基于PyTorch深度学习遥感影像地物分类与目标检测、分割及遥感影像问题深度学习优化
Findcontours and drawcontours & image contours (I) & cv2 boundingRect and cv2. rectangle
How do you choose sentinel vs. hystrix?
Pyinstaller packaging & problem solving about enum34 & reduced version
MySQL insert ignore and replace into
Data transfer principle between TX2 video memory and memory
SYSTEMd management process exporter
随机推荐
ADVANCE.AI:目前进入拉美地区的中国企业还较少,值得探索
【學習筆記】帶你從0開始學習 01Trie
人力资源管理软件让每位员工的记录触手可及
标签平滑(LabelSmoothing)介绍与代码实现
你为什么会做测试/开发程序员?各路伙伴描述......
双指针(一)
OSPF comprehensive experiment
接口测试经典面试题:Session、cookie、token有什么区别?
(pc+wap) dream weaving template protective mask website
Light up through UDP
MySQL插入数据insert ignore和replace into
Search for matching files or directories under folders glob ()
tsconfig. Role of JSON file
TX2显存与内存之间数据传递原理
Service worker guide-1
Pytorch训练模型固定随机种子(seed),保证精度可复现
【学习笔记】带你从0开始学习 01Trie
MySQL prefix index
PHP生成器的使用yield性能优化
Based on pytorch deep learning, remote sensing image feature classification and target detection, segmentation and remote sensing image problem deep learning optimization