We have a lot of ways to solve the maximum subsequence sum problem, but different ways take different time.

1、Brute-force algorithm

int maxSubSum1(const vector<int> &a)
{
int maxSum=0; for(int i=0;i<a.size();i++)
for(int j=i;j<a.size();j++)
{
int sum=0;
for(int k=i;k<=j;k++)
sum+=a[k]; if(sum>maxSum)
maxSum=sum;
} return maxSum;
}
/*The running time is O(n^3)
It takes too much time.
*/

2、a little imporvement

int maxSubSum2(const vector<int>& a )
{
int maxSum=0; for(int i=0;i<a.size();i++)
{
int sum=0; for(int j=i;j<a.size();j++)
{
sum+=a[j];
if(maxSum<sum)
{
maxSum=sum;
}
}
} return maxSum;
}

3. Divide-conquer algorithm

We can divide this problem into three parts:
(1) First half;

(2) cross the middle parts;

(3) second part;

What we need to do is to find the max sum of the three part.

int max3(int a, int b, int c)
{
if(a>b)
{
if(a>c)return a;
else return c;
}
else
{
if(c>b)return c;
else return b;
}
} int maxSubSum3(cosnt vector<int >& a, int left, int right)
{
if(left==right)
if(a[left]>0) return a[left];
else return 0; int center= (left+right)/2;
int maxLeftSum=maxSumRec(a, left, center);
int maxRightSum=maxSumRec(a, center+1, right); int maxLeftBoderSum=0, leftBoderSum=0;
for(int i=center;i>=left;i--)
{
leftBoderSum+=a[i];
if(leftBoderSum>maxLeftBoderSum)
maxLeftBoderSum=leftBoderSum;
} int maxRightBoderSum=0, leftBoderSum=0;
for(int i=center+1;i<=right;i++)
{
rightBoderSum+=a[i];
if(rightBoderSum>maxRightBoderSum)
maxRightBoderSum=rightBoderSum;
} return max3(maxLeftSum, maxLeftBoderSum+maxRightBoderSum,maxRightSum);
}

4. The best algorithm

If the start is negative, the sum of the subsequence can not be the max. Hence, any negative subsequence cannot possibly be a prefix of the optimal subsequence.

int maxSubSum4(const vector<int> & a)
{
int maxSum=0, sum=0; for(int i=0;i<a.size();i++)
{
sum+=a[i]; if(sum>maxSum)
maxSum=sum;
else if(sum<0)
sum=0;
} return maxSum;
}

  

Maxmum subsequence sum problem的更多相关文章

  1. Solutions for the Maximum Subsequence Sum Problem

    The maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional ...

  2. MAXIMUM SUBSEQUENCE SUM PROBLEM

    排除不合理的项(负值), 设定一个标杆sum, 往后扫描看是否有比sum好的情况. We should ensure the following conditions: 1. The result m ...

  3. HD2058The sum problem

    The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  4. HDU 2058 The sum problem(枚举)

    The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...

  5. HDU 2058:The sum problem(数学)

    The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 【BZOJ-3638&3272&3267&3502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广

    3638: Cf172 k-Maximum Subsequence Sum Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 174  Solved: 9 ...

  7. summary of k Sum problem and solutions in leetcode

    I found summary of k Sum problem and solutions in leetcode on the Internet. http://www.sigmainfy.com ...

  8. Subset sum problem

    https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...

  9. HDu 1001 Sum Problem 分类: ACM 2015-06-19 23:38 12人阅读 评论(0) 收藏

    Sum Problem Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

随机推荐

  1. 第9章 创建Web数据库

    1.登录MySQL: mysql -h hostname -u username -p password *-h 用于指定所希望连接的主机,即运行MySQL服务器的机器: -u 用于指定连接数据库时使 ...

  2. FZU 2088 最长队名

    Problem 2088 最长队名  Problem Description Jack所在的班级决定组团报名参加FZU校赛.为了体现班级的团结和睦,班长决定用班级所有人的名字连起来组成一个史上最长最醒 ...

  3. Git创建空白新分支

    向分支提交一个初始的空commit,保证完全复位. 创建并切换新分支 git branch <new_branch> git checkout <new_branch> git ...

  4. Unity3DGUI:常用控件

  5. iOS 消息推送证书生成方法的简单说明

    openssl x509 -in idp.flowtreasure.cer -inform der -out PushChatCert.pem openssl pkcs12 -nocerts -out ...

  6. MySql 如何实现不同数据库同步【2个】

    环境要求: Windows 操作系统 需要Mysql 3.23.15以后的版本. 假设数据库A为主机,数据库B为从机(A向B提供同步服务,即B中的数据来自A) A机器:IP=10.10.151.166 ...

  7. docker容器安全

    title: docker容器安全 tags: Docker,容器,安全策略 grammar_cjkRuby: true --- Docker容器的安全性 1.安全策略-Cgroup 1.限制Cpu ...

  8. html-----vedio标签(HTML5新标签VIDEO在IOS上默认全屏播放)

    今天做一个app时发现一个问题,应用html5中的video标签加载视频,在Android手机上默认播放大小,但是换成iPhone手机上出问题了,默认弹出全屏播放,查找了好多论坛,都没有谈论这个的.然 ...

  9. String、StringBuffer和StringBuilder区别及性能分析

    1.性能比较:StringBuilder >  StringBuffer  >  String 2.String <(StringBuffer,StringBuilder)的原因 S ...

  10. vga显示彩条

    vga显示驱动程序可分为扫描行列和行列同步两个部分 //注意:只有在有效区域内给vga赋值才会有颜色变化 assign vga_b = isready ? vga_s[:] :'d0; assign ...