-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_16.17_maxSubArray.cc
52 lines (47 loc) · 1.36 KB
/
Problem_16.17_maxSubArray.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <algorithm>
#include <vector>
using namespace std;
class Solution
{
public:
int maxSubArray1(vector<int>& nums)
{
int n = nums.size();
// dp[i]表示的含义为,必须以nums[i]为结尾的序列最大和
vector<int> dp(n);
dp[0] = nums[0];
int ans = dp[0];
for (int i = 1; i < n; i++)
{
dp[i] = dp[i - 1] > 0 ? dp[i - 1] + nums[i] : nums[i];
ans = std::max(ans, dp[i]);
}
return ans;
}
class Info
{
public:
int lSum; // 表示[l, r]区间内必须以l为左端点的最大子段和
int rSum; // 表示[l, r]区间内必须以r为右端点的最大子段和
int mSum; // 表示[l, r]区间内最大子段和
int iSum; // 表示[l, r]区间和
};
Info f(vector<int>& arr, int l, int r)
{
if (l == r)
{
return {arr[l], arr[l], arr[l], arr[l]};
}
int m = (r - l) / 2 + l;
Info left = f(arr, l, m);
Info right = f(arr, m + 1, r);
int lSum = std::max(left.lSum, left.iSum + right.lSum);
int rSum = std::max(right.rSum, right.iSum + left.rSum);
int iSum = left.iSum + right.iSum;
// 两种情况,包含中点,不包含中点
int mSum = std::max({left.mSum, right.mSum, left.rSum + right.lSum});
return {lSum, rSum, mSum, iSum};
}
// 分治法
int maxSubArray2(vector<int>& nums) { return f(nums, 0, nums.size() - 1).mSum; }
};