1. Title
1.1 English title
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
1.2 Chinese title
Given an integer array nums, find a continuous sub array with the largest sum (the sub array contains at least one element) and return its maximum sum.
1.3 input and output
input | output |
---|---|
nums = [-2,1,-3,4,-1,2,1,-5,4] | 6 |
nums = [1] | 1 |
nums = [5,4,-1,7,8] | 23 |
1.4 constraints
- 1 <= nums.length <= 3 * 104
- -105 <= nums[i] <= 105
2. Experimental platform
IDE:VS2019
IDE version: 16.10.1
Language: C + + 11
3. Procedure
3.1 test procedure
#include "Solution.h"
#include // std::vector
#include // std::cout
using namespace std;
//Main program
void main()
{
//Input
vector nums = { -100000 };
Solution solution; // Instantiate solution
int k = solution. maxSubArray(nums); // Main function
//Output
cout << k << endl;
}
3.2 function program
3.2.1 exhaustive calendar
(1) Code
#pragma once
#include // std::vector
using namespace std;
//Main function
class Solution {
public:
int maxSubArray(vector& nums) {
//Violence solving
int maxValue = -100000;
for (int i = 0; i < nums.size(); I + +) // traverse the starting value
{
int nowSub = 0;
for (int j = i; j < nums.size(); J + +) // go through all
{
nowSub += nums[j];
if (nowSub > maxValue) maxValue = nowSub;
}
}
return maxValue;
}
};
(2) Interpretation
#include "Solution.h"
#include // std::vector
#include // std::cout
using namespace std;
//Main program
void main()
{
//Input
vector nums = { -100000 };
Solution solution; // Instantiate solution
int k = solution. maxSubArray(nums); // Main function
//Output
cout << k << endl;
}
#pragma once
#include // std::vector
using namespace std;
//Main function
class Solution {
public:
int maxSubArray(vector& nums) {
//Violence solving
int maxValue = -100000;
for (int i = 0; i < nums.size(); I + +) // traverse the starting value
{
int nowSub = 0;
for (int j = i; j < nums.size(); J + +) // go through all
{
nowSub += nums[j];
if (nowSub > maxValue) maxValue = nowSub;
}
}
return maxValue;
}
};
This method is the easiest method to think of. It uses the sliding window method for traversal, obtains the sequence starting with a certain sequence, calculates the maximum value, and updates the maximum value in real time with the traversal. Complexity is o(\(n^2\))。
3.2.2 dynamic programming method
(1) Code
#pragma once
#include // std::vector
using namespace std;
//Main function
class Solution {
public:
int maxSubArray(vector& nums) {
//Dynamic programming (time complexity O (n), space complexity O (n))
int length = nums.size();
vector dp(length); // Stores the maximum value per recursion
dp[0] = nums[0];
for (int i = 1; i < length; i++)
dp[i] = max(dp[i - 1] + nums[i], nums[i], [](int a, int b) {return a > b ? a : b; }); // Lamda expression
//Find the maximum value in DP
int maxSub = -100000;
For (auto J: DP) // range based for loop in C + + 11
if (maxSub < j)
dp[j] = maxSub;
return maxSub;
}
};
(2) Train of thought
reference resources:https://zhuanlan.zhihu.com/p/85188269
3.2.3 Kadane algorithm
(1) Code
#pragma once
#include // std::vector
using namespace std;
//Main function
class Solution {
public:
int maxSubArray(vector& nums) {
//Kadane algorithm (time complexity O (n), space complexity O (1))
int length = nums.size();
int maxSub = nums[0]; // Slow pointer
int maxSubTemp = nums[0]; // Quick pointer
for (auto i : nums)
{
maxSubTemp = max(maxSubTemp + nums[i], nums[i], [](int a, int b) {return a > b ? a : b; }); // Lamda expression
If (maxsubtemp > maxsub) // if the current maximum value is greater than the total maximum value, the total maximum value will be updated
maxSub = maxSubTemp;
}
return maxSub;
}
};
(2) Interpretation
Kadane algorithm is based on the dynamic programming method and the fast and slow pointer method. The fast pointer points to the sum of the maximum values of the subarray ending with I, and the slow pointer points to the sum of the maximum values of the subarray so far
3.3.4 divide and conquer
(1) Code
pragma once
include // std::vector
//#include// INT_ Min integer minimum
include // std::max
using namespace std;
//Main function
class Solution {
public:
int maxSubArray(vector& nums) {
if (nums.empty()) return 0;
return helper(nums, 0, (int)nums.size() – 1);
}
int helper(vector& nums, int left, int right)
{
if (left >= right) return nums[left];
int mid = left + (right – left) / 2;
int lmax = helper(nums, left, mid – 1);
int rmax = helper(nums, mid + 1, right);
int mmax = nums[mid], t = mmax;
for (int i = mid – 1; i >= left; –i)
{
t += nums[i];
mmax = max(mmax, t);
}
t = mmax;
for (int i = mid + 1; i <= right; ++i)
{
t += nums[i];
mmax = max(mmax, t);
}
return max(mmax, max(lmax, rmax));
}
};
reference resources:https://www.cnblogs.com/grandyang/p/4377150.html
(2) Interpretation
reference resources:https://www.jianshu.com/p/3a38d523503b
4. Relevant knowledge
(1) Sliding window method
Sliding window is actually selecting part of the sequence as the window. The window keeps moving until you find the answer. It feels more like an idea.
For details, please refer to:https://www.cnblogs.com/huansky/p/13488234.html
(2) Lamda expression
Lamda expression can directly define short and concise functions where functions need to be called without pre-defined functions, but it is not easy to reuse. It is suitable for relatively simple functions that do not need to be reused. Written as:
Func (input1, input2, [], (Type1 Parameter1, type2 parameter2) {function;})
Detailed introduction reference:https://blog.csdn.net/A1138474382/article/details/111149792
(3) Range based for loop
The new feature added in C + + 11 is similar to the for loop of object-oriented languages such as Python and MATLAB. The writing method is as follows:
for(auto i:array){;}
Detailed introduction reference:https://blog.csdn.net/hailong0715/article/details/54172848
(4) Kadane algorithm
reference resources:https://zhuanlan.zhihu.com/p/85188269