给定一个整数数组,找出总和最大的连续数列,并返回总和。
示例:
输入: [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
进阶:
O(n) 的解法,尝试使用更为精妙的分治法求解。 使用动态规划可以实现 O(n) 的复杂度。使用 max 记录以 j 结尾的最大连续和,ans 记录从 0 ~ j 中的最大的连续数列和(不一定以 j 结尾),其递推关系为:
m
a
x
[
j
]
=
M
A
X
{
m
a
x
[
j
−
1
]
+
n
u
m
s
[
j
]
,
m
a
x
[
j
−
1
]
≥
0
n
u
m
s
[
j
]
,
m
a
x
[
j
−
1
]
<
0
a
n
s
[
j
]
=
M
A
X
{
m
a
x
[
j
]
,
c
h
o
o
s
e
j
a
n
s
[
j
−
1
]
,
n
o
t
c
h
o
o
s
e
每次纳入 nums[j] 并考虑:
max[j - 1] < 0,则直接从 j 开始就好,即设置 max[j] = 0。因为算上前面的序列,和只会更小。max[j - 1] >= 0,则 max[j - 1] 直接加上 nums[j] 就是以 j 结尾的最大连续和 max[j],左端起点不会发生改变。max[j] 与 ans[j - 1] 比较,ans[j] 结果取最大值。 理论上需要开辟一个 O(n) 数组存储 max,但是由于只需要求 max 的最大值 ans,因此可以边计算边更新 ans,省去了 O(n) 的空间。
public class Solution {
public int MaxSubArray(int[] nums) {
int ans = nums[0], max = ans;
for (var j = 1; j < nums.Length; j++) {
if (max < 0) max = 0; // 先前的序列如果 < 0,则直接抛弃,从第 j 位开始重新计数
max += nums[j]; // 并入第 j 位
if (max > ans) ans = max; // 更新结果
}
return ans;
}
}
使用分治可以实现 O(logn) 的复杂度。将数组 nums 一分为二,记为 left 和 right。则 nums 的答案 Max 可能有如下 3 中情况:



因此,需要记录区间的左端最大连续和 LMax(红色) 与右端最大连续和 RMax(黄色),其对应的更新情况如下:


public class Solution {
public struct Range
{
public int LMax; // 从左端开始的最长连续和
public int RMax; // 以右端结尾的最长连续和
public int Sum; // 区间总和
public int Max; // 区间内最长连续和
public Range(int l, int r, int s, int m) {
LMax = l;
RMax = r;
Sum = s;
Max = m;
}
public static Range operator +(Range left, Range right) {
int lMax = Math.Max(left.LMax, left.Sum + right.LMax);
int rMax = Math.Max(right.RMax, left.RMax + right.Sum);
int sum = left.Sum + right.Sum;
int max = Math.Max(Math.Max(left.Max, right.Max), left.RMax + right.LMax);
return new Range(lMax, rMax, sum, max);
}
}
public int MaxSubArray(int[] nums) {
return Partition(nums, 0, nums.Length - 1).Max;
}
public Range Partition(int[] nums, int i, int j) {
if (i == j) return new Range(nums[i], nums[i], nums[i], nums[i]);
int mid = (i + j) >> 1;
return Partition(nums, i, mid) + Partition(nums, mid + 1, j);
}
}