当前位置:网站首页>Maximum sum of continuous subarray of sword finger offer (2)
Maximum sum of continuous subarray of sword finger offer (2)
2022-07-26 18:21:00 【Morita Rinko】
The maximum sum of successive subarrays ( Two )
Subject requirements :
Enter a length of n Integer array array, One or more consecutive integers in an array form a subarray , Find a continuous subarray with the largest sum .
1. Subarray is continuous , such as [1,3,5,7,9] The subarrays of are [1,3],[3,5,7] wait , however [1,3,7] It's not a subarray
2. If there are multiple consecutive subarrays with the largest sum , Then return the longest , The data of this question ensure that there is only one
3. The minimum length of the subarray defined by this question is 1, There is no empty subarray , That there is no [] Is a sub array of an array
4. The returned array is not included in the space complexity calculation 
Answer key :
The title has two requirements :
- Calculate and record the sum of the largest array
- Record the starting and ending positions of the largest array sum
The key :
- use dp Array to store the maximum array sum of each element in the solution array
- use maxsum Variable to store the largest array and
- use left、right Variable to record the starting position and target position of the sliding array ( That is, the starting and ending positions of the array that is calculating the sum )
- use resl、resr Variable to record the starting and ending positions of the longest array of the current maximum array and
- If dp The sum of the array stored in the array is greater than maxsum, Or equal to maxsum And the length is greater than the longest array of the current stored maximum array and , Update resl、resr and maxsum Value
- If the current element value is added to the previous array and , Note that the sum of the previous array is negative , The maximum array sum should be restarted from the current element , therefore dp[i] The value of should be the value of the current element , also left=i;
Solution code :
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
*
* @param array int integer vector
* @return int integer vector
*/
vector<int> FindGreatestSumOfSubArray(vector<int>& array) {
// write code here
vector<int> dp(array.size(),0);
int left=0,right=0,resl=0,resr=0;
dp[0]=array[0];
int maxsum=array[0];
for(int i=1;i<array.size();i++){
right++;
dp[i]=max(dp[i-1]+array[i],array[i]);
if(dp[i-1]+array[i]<array[i]){
left=i;
}
// If you add i The value of is greater than or equal to the maximum value, but the length is greater than the length of the maximum array
if(dp[i]>maxsum || (dp[i]==maxsum && (right-left)>(resr-resl))){
maxsum=dp[i];
resl=left;
resr=right;
}
}
vector<int> resarray(array.begin()+resl,array.begin()+resr+1);
return resarray;
}
};
边栏推荐
- 【一知半解】线程池
- 详解 gRPC 客户端长连接机制实现
- LeetCode50天刷题计划(Day 2—— 无重复字符的最长子串 10.00-12.00)
- 236. The nearest common ancestor of a binary tree
- 立即报名 | 云原生技术交流 Meetup 广州站已开启,8 月 6 号与你相遇!
- Rookie cpaas platform microservice governance practice
- 网易游戏研发工程师实习生(客户端方向)一面
- SQL判断某列中是否包含中文字符、英文字符、纯数字,数据截取
- 网上炒股,选择在哪里开户比较安全呢?
- 有一说一,阿里P7的薪资待遇是真的香
猜你喜欢

ICML 2022(第四篇)|| 图分层对齐图核实现图匹配

Deep learning experiment: softmax realizes handwritten digit recognition

俄语翻译的就业前景怎样 如何做好俄语翻译工作

8.1 Diffie Hellman key exchange

AI zhetianchuan ml integrated learning

Relative path and absolute path

Vector CANape - How to Send Receive CAN Message in CANape
![[ Kitex 源码解读 ] 服务发现](/img/70/c74ede02b794e586d629876d2b2376.png)
[ Kitex 源码解读 ] 服务发现
![[metauniverse OMI theory] analyze Web3 risk challenges and build Web3 ecological security](/img/d1/4a424d810f7a6aaccae80b4fe232c0.png)
[metauniverse OMI theory] analyze Web3 risk challenges and build Web3 ecological security

链表-反转链表
随机推荐
Leetcode 50 day question brushing plan (day 1 - add two numbers 11.00-12.30)
Deep learning experiment: softmax realizes handwritten digit recognition
8.1 Diffie-Hellman密钥交换
ssm练习第三天_分页助手_安全框架
[brother hero July training] day 25: tree array
LeetCode 0139. 单词拆分
剑指offer 连续子数组的最大和(二)
The second set of 2020 American Asian individual match
Leetcode 50 day question brushing plan (day 2 - the longest substring without repeated characters 10.00-12.00)
ICML 2022(第四篇)|| 图分层对齐图核实现图匹配
Detailed explanation of openwrt's feeds.conf.default
“蔚来杯“2022牛客暑期多校训练营3记录
相对路径与绝对路径
[unity3d] rocker
4、 Service communication principle, code implementation
Are you suitable for automated testing?
7月30号PMP考试延期后我们应该做什么?
openssl
[training day3] section
网易游戏研发工程师实习生(客户端方向)一面