Maxmum subsequence sum problem
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的更多相关文章
- Solutions for the Maximum Subsequence Sum Problem
The maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional ...
- MAXIMUM SUBSEQUENCE SUM PROBLEM
排除不合理的项(负值), 设定一个标杆sum, 往后扫描看是否有比sum好的情况. We should ensure the following conditions: 1. The result m ...
- HD2058The sum problem
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU 2058 The sum problem(枚举)
The sum problem Problem Description Given a sequence 1,2,3,......N, your job is to calculate all the ...
- HDU 2058:The sum problem(数学)
The sum problem Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【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 ...
- 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 ...
- Subset sum problem
https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...
- 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 ...
随机推荐
- Oracle与Sqlserver:Order by NULL值介绍
针对页面传参到in的子集中去进行查询操作的话,就会有in(xxx,null),这样就会导致查询的结果中其实直接过滤掉了null,根本就查不出来null的值.之前对于null的操作都是进行不同数据库的n ...
- mysql的数据类型与列属性
- laravel-1 安装.配置
听说laravel一直是一个很牛B的框架,之前接触过tp ci 也还只是一个小白,具体的核心没搞过,但对于我来说,框架都是拿来用的,会用即可. 以下内容为观看视频和自己查看资料后的整理,方便大家和自己 ...
- 自定义实现IEnumerable
Demo: http://files.cnblogs.com/files/georgeHeaven/Demo.IEnumerable.rar 一.使用场景 在开发过程中,经常需要使用foreach来循 ...
- Swift原理
背景与概览 Swift 最初是由 Rackspace 公司开发的高可用分布式对象存储服务,并于 2010 年贡献给 OpenStack 开源社区作为其最初的核心子项目之一,为其 Nova 子项目提供虚 ...
- 51nod1092(lcs简单运用/dp)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 题意:中文题诶- 思路: 解法1:最坏的情况就是在原字 ...
- 基于React Native的58 APP开发实践
React Native在iOS界早就炒的火热了,随着2015年底Android端推出后,一套代码能运行于双平台上,真正拥有了Hybrid框架的所有优势.再加上Native的优秀性能,让越来越多的公司 ...
- 专注VR/AR广告 ,内容感知广告公司Uru获80万美元投资
随着AR/VR技术不断地跃进,越来越多的公司开始运用这项技术为消费者提供广告和营销信息.Uru是一家打造计算机视觉驱动内容广告的公司,专注于数字视频和VR/AR类似的沉浸式媒介,就在刚刚这家公司宣布完 ...
- 子查询 此处该用AND 而不是 WHERE
条件:有一张账户表,一张订单表. 需求:求出所有role = 2 即客服人员,所有操作成功的订单数量.结果:能查出所有的客服人员名称,以及操作的订单数量(关键点在于,没有操作过订单,则数量显示为0) ...
- 读取HttpWebResponse流的两种方法及注意的问题
1. 获取流 HttpWebRequest request= (HttpWebRequest)WebRequest.Create(uri); //构建http request request ...