今天分享的算法问题是利用三种方法实现最大子段和。有任何错误恳请大家批评指正。
问题:已知一段数字序列,如何计算出最大的子段和
拿到这个问题第一个想到的就是暴力三循环方法,时间复杂度为O()
int maxsum1(vector<int> a) {
//暴力循环解法
int sum = 0;
for (int i = 0; i < a.size(); i++) {
for (int j = i + 1; j < a.size(); j++) {
int thissum = 0; //计算[i,j]的子段和
for (int k = i; k <= j; k++) {
thissum += a[k];
}
if (thissum > sum) {
sum = thissum;
}
}
}
return sum;
}
这个方法虽然简单,但是耗时实在太久,如果数组过大的情况下很容易造成时间超限。
为此,我们可以尝试利用分治的方法。设start和end为最大子段和开始和结束的元素。我们将数组平分为left和right。分治情况如下所示:
start,end 都在left |
start,end 都在right |
start在left, end在right |
我们只需要对这三种情况进行讨论即可。
int maxsum2(vector<int> a, int left, int right) {
//利用分治法求解最大子段和
int sum = 0;
if (left == right) {
sum = a[left] > 0 ? a[left] : 0;
}
else {
int center = (right - left) / 2 + left;
int sumleft = maxsum2(a, left, center);
int sumright = maxsum2(a, center + 1, right);
int s1 = 0;
int s2 = 0;
int lefts = 0;
int rights = 0;
for (int i = center; i >= left; i--) {
lefts += a[i];
if (lefts > s1) {
s1 = lefts;
}
}
for (int i = center+1; i <= right; i++) {
rights += a[i];
if (rights > s2) {
s2 = rights;
}
}
sum = s1 + s2;
if (sum < sumleft) {
sum = sumleft;
}
if (sum < sumright) {
sum = sumright;
}
}
return sum;
}
由于用到了递归,所以时间开销仍然很大。
下面展示的是最简单的动态规划方法:首先定义当前子段和为b,最大子段和为sum。在向当前子段引入新元素的过程中,必须首先保证当前子段和必须是正数,才能确保新元素加入的前提下当前子段有可能成为最大子段。否则,将当前子段与最大子段和进行比较并更新。
int maxsum3(vector<int> a) {
//利用动态规划
int b = 0;
int sum = 0;
for (int i = 0; i < a.size(); i++) {
if (b > 0) {
b += a[i];
}
else {
b = a[i];
}
if (b > sum) {
sum = b;
}
}
return sum;
}
完整代码和测试如下:
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
vector<int> list = { -2,11,-4,13,-5,-2 };
int maxsum1(vector<int> a) {
//暴力循环解法
int sum = 0;
for (int i = 0; i < a.size(); i++) {
for (int j = i + 1; j < a.size(); j++) {
int thissum = 0; //计算[i,j]的子段和
for (int k = i; k <= j; k++) {
thissum += a[k];
}
if (thissum > sum) {
sum = thissum;
}
}
}
return sum;
}
int maxsum2(vector<int> a, int left, int right) {
//利用分治法求解最大子段和
int sum = 0;
if (left == right) {
sum = a[left] > 0 ? a[left] : 0;
}
else {
int center = (right - left) / 2 + left;
int sumleft = maxsum2(a, left, center);
int sumright = maxsum2(a, center + 1, right);
int s1 = 0;
int s2 = 0;
int lefts = 0;
int rights = 0;
for (int i = center; i >= left; i--) {
lefts += a[i];
if (lefts > s1) {
s1 = lefts;
}
}
for (int i = center+1; i <= right; i++) {
rights += a[i];
if (rights > s2) {
s2 = rights;
}
}
sum = s1 + s2;
if (sum < sumleft) {
sum = sumleft;
}
if (sum < sumright) {
sum = sumright;
}
}
return sum;
}
int maxsum3(vector<int> a) {
//利用动态规划
int b = 0;
int sum = 0;
for (int i = 0; i < a.size(); i++) {
if (b > 0) {
b += a[i];
}
else {
b = a[i];
}
if (b > sum) {
sum = b;
}
}
return sum;
}
void test() {
clock_t start1 = clock();
int sum1 = maxsum1(list);
clock_t end1 = clock();
cout << "调用方法为暴力循环法,最大子段和为 " << sum1 << ",用时为 " << static_cast<double>(end1 - start1) << endl;
clock_t start2 = clock();
int sum2 = maxsum2(list,0,list.size()-1);
clock_t end2 = clock();
cout << "调用方法为分治法,最大子段和为 " << sum2 << ",用时为 " << static_cast<double>(end2 - start2) << endl;
clock_t start3 = clock();
int sum3 = maxsum3(list);
clock_t end3 = clock();
cout << "调用方法为动态规划法,最大子段和为 " << sum3 << ",用时为 " << static_cast<double>(end3 - start3) << endl;
}
int main() {
test();
}