面试刷题 - 最大连续子段和
OJ链接
LEETCODE代码
解法一
线性扫一遍
- 计算前缀和Ai,即a1+a2+……+ai
- 计算最小前缀和Bi,min(A1, A2, ……, Ai)
- 则以ai结尾的最大子段和Ci=Ai-Bi
- 另外开一个变量res,记录Ci的最小值即可
- 时间复杂度O(n)
typedef int ll;
class Solution {
public:
ll maxSubArray(vector<int>& nums) {
int sz = nums.size();
if (!sz) {
return 0;
}
ll prefix_sum = nums[0];
ll min_prefix_sum = nums[0];
ll res = nums[0];
for(int i=1; i<sz; i++) {
prefix_sum += nums[i];
res = max(res, prefix_sum- min(min_prefix_sum, 0));
min_prefix_sum = min(min_prefix_sum, prefix_sum);
}
return res;
}
};
解法二
动态规划
- 维护以ai结尾的最大子段和Ci
- 另外开一个变量res,记录Ci的最小值即可
- 时间复杂度O(n)
typedef int ll;
class Solution {
public:
ll maxSubArray(vector<int>& nums) {
int sz = nums.size();
if (sz == 0) {
return 0;
}
ll res, current_max;
res = current_max = nums[0];
for (int i=1; i<sz; i++) {
current_max = max(nums[i], current_max + nums[i]);
res = max(res, current_max);
}
return res;
}
};
解法三
分治法
- 对于每一段,需要维护
- 从最左侧开始的最大子段和
- 从最右侧开始的最大子段和
- 最大子段和
- 总和
- 时间复杂度O(n)
T(n) = 2T(n/2) + O(1) = 2*2T(n/2/2) + O(1) + O(1) = 2^tT(n/(2^t)) + tO(1) = 2^t + tO(1)
2^t = n
t = logn
T(n) = O(n + logn) = O(n)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
ll left_sum, right_sum, total_sum;
return maxSubArray(nums, 0, nums.size() - 1, left_sum, right_sum, total_sum);
}
private:
// 每一段需要维护
// 总和、左侧最大和、右侧最大和,最大子段和
ll maxSubArray(vector<int>& nums, int L, int R, ll &left_sum, ll &right_sum, ll &total_sum) {
if (L>R) {
return 0;
}
if (L==R) {
left_sum = right_sum = total_sum = nums[L];
return nums[L];
}
int mid = L + R >> 1;
ll left_left_sum, left_right_sum, left_total_sum;
ll right_left_sum, right_right_sum, right_total_sum;
ll left_max_sum = maxSubArray(nums, L, mid, left_left_sum, left_right_sum, left_total_sum);
ll right_max_sum = maxSubArray(nums, mid+1, R, right_left_sum, right_right_sum, right_total_sum);
left_sum = max(left_left_sum, left_total_sum + right_left_sum);
right_sum = max(right_right_sum, right_total_sum + left_right_sum);
total_sum = left_total_sum + right_total_sum;
return max(max(left_max_sum, right_max_sum), left_right_sum + right_left_sum);
}
};
解法四
分治法
- 但是不维护解法三中那一堆数据
- 每次求跨中间最大子段和时线性扫描
- 时间复杂度O(nlogn)
typedef int ll;
class Solution {
public:
int maxSubArray(vector<int>& nums) {
return maxSubArray(nums, 0, nums.size() - 1);
}
private:
// 每一段需要维护
// 总和、左侧最大和、右侧最大和,最大子段和
ll maxSubArray(vector<int>& nums, int L, int R) {
if (L>R) {
return 0;
}
if (L==R) {
return nums[L];
}
int mid = L + R >> 1;
ll left_max_sum = maxSubArray(nums, L, mid);
ll right_max_sum = maxSubArray(nums, mid+1, R);
ll res = max(left_max_sum, right_max_sum);
ll tmp_left = nums[mid], rec_left = nums[mid];
for (int i=mid-1; i>=L; i--) {
tmp_left += nums[i];
rec_left = max(tmp_left, rec_left);
}
ll tmp_right = nums[mid+1], rec_right = nums[mid+1];
for (int i=mid+2; i<=R; i++) {
tmp_right += nums[i];
rec_right = max(tmp_right, rec_right);
}
res = max(res, rec_left + rec_right);
return res;
}
};
洛谷OJ测试
解法 | 用时(ms) | 内存(MB) |
---|---|---|
一 | 149 | 1.87 |
二 | 150 | 1.79 |
三 | 168 | 2.19 |
四 | 225 | 1.82 |