最大连续子序列和问题是个很老的面试题了,最佳的解法是O(N)复杂度,当然其中的一些小的地方还是有些值得注意的地方的。这里还是总结三种常见的解法,重点关注最后一种O(N)的解法即可。需要注意的是有些题目中的最大连续子序列和如果为负,则返回0;而本题目中的最大连续子序列和并不返回0,如果是全为负数,则返回最大的负数即可。
问题描述
求取数组中最大连续子序列和,例如给定数组为A={1, 3, -2, 4, -5}, 则最大连续子序列和为6,即1+3+(-2)+ 4 = 6。
解法1—O(N^2)解法
因为最大连续子序列和只可能从数组0到n-1中某个位置开始,我们可以遍历0到n-1个位置,计算由这个位置开始的所有连续子序列和中的最大值。最终求出最大值即可。
更详细的讲,就是计算从位置0开始的最大连续子序列和,从位置1开始的最大连续子序列和。。。直到从位置n-1开始的最大连续子序列和,最后求出所有这些连续子序列和中的最大值就是答案。
解法2—O(NlgN)解法
该问题还可以通过分治法来求解,最大连续子序列和要么出现在数组左半部分,要么出现在数组右半部分,要么横跨左右两半部分。因此求出这三种情况下的最大值就可以得到最大连续子序列和。
解法3—O(N)解法
还有一种更好的解法,只需要O(N)的时间。因为最大 连续子序列和只可能是以位置0~n-1中某个位置结尾。当遍历到第i个元素时,判断在它前面的连续子序列和是否大于0,如果大于0,则以位置i结尾的最大连续子序列和为元素i和前面的连续子序列和相加;否则,则以位置i结尾的最大连续子序列和为元素i。
参考http://blog.csdn.net/ssjhust123/article/details/8032464
例题:
-
题目描述:
-
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和为20。现在增加一个要求,即还需要输出该子序列的第一个和最后一个元素。
-
输入:
-
测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( K< 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
-
输出:
-
对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
-
样例输入:
-
6 -2 11 -4 13 -5 -2 10 -10 1 2 3 4 -5 -23 3 7 -21 6 5 -8 3 2 5 0 1 10 3 -1 -5 -2 3 -1 0 -2 0
-
样例输出:
-
20 11 13 10 1 4 10 3 5 10 10 10 0 -1 -2 0 0 0
http://ac.jobdu.com/problem.php?pid=1011
AC code:
#define RUN
#ifdef RUN
/**
http://ac.jobdu.com/problem.php?pid=1011
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <cctype>
#include <algorithm>
#include <utility>
#include <math.h>
using namespace std;
#define MAXN 10001
int buf[MAXN];
//O(n2)
void maxsequence(int n){
int max_ = buf[0];
int max_start = buf[0];
int max_end = buf[0];
for(int i=0; i<n; i++){
int sum_ = 0;
for(int j=i; j<n; j++){
sum_ += buf[j];
if(sum_ > max_){
max_ = sum_;
max_start = buf[i];
max_end = buf[j];
}
}
}
printf("%d %d %d\n", max_, max_start, max_end);
}
/*求三个数最大值*/
int max3(int i, int j, int k){
int max_ = i;
if(j > max_){
max_ = j;
}
if(k > max_){
max_ = k;
}
return max_;
}
int maxsequence2(int a[], int l, int u){
if(l > u){
return 0;
}
if(l == u){
return a[0];
}
int m = (l+u)/2;
// 求横跨左右的最大连续子序列左半部分
int lmax = a[m], lsum = 0;
for(int i=m; i>=0; i--){
lsum += a[i];
if(lsum > lmax){
lmax = lsum;
}
}
/*求横跨左右的最大连续子序列右半部分*/
int rmax = a[m+1], rsum = 0;
for(int i=m+1; i<=u; i++){
rsum += a[i];
if(rsum > rmax){
rmax = rsum;
}
}
return max3(lmax+rmax, maxsequence2(a, 0, m), maxsequence2(a, m+1, u));
}
void maxsequence3(int a[], int len)
{
int maxsum, maxhere;
maxsum = maxhere = a[0]; //初始化最大和为a[0]
int max_start = buf[0];
int max_end = buf[0];
int tmp = buf[0];
for (int i=1; i<len; i++) {
if (maxhere < 0){
maxhere = a[i]; //如果前面位置最大连续子序列和小于等于0,则以当前位置i结尾的最大连续子序列和为a[i]
tmp = a[i];
}
else{
maxhere += a[i]; //如果前面位置最大连续子序列和大于0,则以当前位置i结尾的最大连续子序列和为它们两者之和
}
if (maxhere > maxsum) {
maxsum = maxhere; //更新最大连续子序列和
max_start = tmp;
max_end = a[i];
}
}
printf("%d %d %d\n", maxsum, max_start, max_end);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("1011.in", "r", stdin);
freopen("1011.out", "w", stdout);
#endif
int n;
while(scanf("%d", &n)==1 && n!=0){
memset(buf, 0, sizeof(buf));
for(int i=0; i<n; i++){
scanf("%d", &buf[i]);
}
//maxsequence(n);
//printf("%d\n", maxsequence2(buf, 0, n-1));
maxsequence3(buf, n);
}
}
#endif
对于解法2仍存在问题,待解决。解法一和三顺利AC