MAXIMUM SUBSEQUENCE SUM PROBLEM
排除不合理的项(负值), 设定一个标杆sum, 往后扫描看是否有比sum好的情况.
We should ensure the following conditions:
1. The result must be the max sum.
2.* If more than one sum is max(same max value), choose the longer sequence.
3.* If more than one sum is max & same length, choose the former sequence.
#!/usr/bin/python2.7
#File: maxChildSum.py
#Author: lxw
#Time: 2014-08-22
#Usage: Find the max sum of a sub-sequence in a sequence.
#NOTE: @1: first, must be the max sum.
# @2: if more than one sum is max(same max value), choose the longer sequence. def main():
#arr = [-12, 0, -14, -14, -13, -14, -2] #"zero" test.
#arr = [0, -14, -14, -13, -14, -2] #another "zero" test.
arr = [-100, -14, -14, -13, -14, -2] #"all negtive" test.
#arr = [-12, 10, 2, -14, -14, 13, -2] #"longer sequence but not equal sum" test.
#arr = [-12, 0, 10, -14, -14, -2] #"as long as better" test.
#arr = [-12, 0, 10, -14, -14, 3, 7, -2] #"same sum & same length" test.
#arr = [-12, 10, -14, -14, 3, 7, -2] #"same sum but longer sequence" test. index = 0
length = len(arr)
while arr[index] < 0:
index += 1
if index == length:
break if index < length:
sum = -1 #This initial value is important.
start = index
temp_sum = 0 #sum
temp_start = index
end = 0 while index < length:
temp_sum += arr[index]
if temp_sum >= 0:
if temp_sum > sum:
sum = temp_sum
start = temp_start
end = index
elif temp_sum == sum:
if start == temp_start:
end = index
elif index - temp_start > end - start:
start = temp_start
end = index
else:
temp_sum = 0
temp_start = index + 1
index += 1
print "max sum:{0:<4}start:{1:<4}end:{2:<4}max length:{3:<4}".format(sum, start, end, end-start+1)
else:
#All the numbers are negative.
print "max sum:{0:<4}start:{1:<4}end:{2:<4}max length:{3:<4}".format(0, 0, 0, 0) if __name__ == '__main__':
main()
else:
print "Being imported as a module."
MAXIMUM 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 ...
- Maxmum subsequence sum problem
We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
随机推荐
- Visual Studio : Debug > Start new instance添加快捷键
Instead of using your Macro, you could simply add in the Options-Dialog under Environment->Keyboa ...
- 181. Flip Bits【easy】
181. Flip Bits[easy] Determine the number of bits required to flip if you want to convert integer n ...
- 使用pycharm手动搭建python语言django开发环境(四) django中buffer类型与str类型的联合使用
在django中,如果用到buffer类型时,buffer的编码格式是utf-8类型.使用str()进行转为字符串类型会异常. 异常会有如下提示:'ascii' codec can't decode ...
- Android学习之ItemTouchHelper实现RecylerView的拖拽以及滑动删除功能
今天在群里见大神们提到控件的拖动以及滑动删除的效果实现,就在网上找了资料ItemTouchHelper学习,并实现其功能.不胜窃喜之至,忍不住跟大家分享一下,如今就对学习过程做下简介.帮助大家实现这样 ...
- UVA 12169 Disgruntled Judge 扩展欧几里得
/** 题目:UVA 12169 Disgruntled Judge 链接:https://vjudge.net/problem/UVA-12169 题意:原题 思路: a,b范围都在10000以内. ...
- GCD - Extreme (II) for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=gcd(i,j); } 推导分析+欧拉函数
/** 题目:GCD - Extreme (II) 链接:https://vjudge.net/contest/154246#problem/O 题意: for(i=1;i<N;i++) for ...
- asp.net c#采集需要登录页面的实现原理及代码
当我们采集页面的时候,如果被采集的网站需要登录才能采集,原理搞清楚了,就好办了,我们所要做的仅仅是在采集的时候(或者说HttpWebRequest提交数据的时候),将Cookie信息放入Http请求头 ...
- MTK驱动探索:mtk6582添加tp驱动s3203
代码从mtk6589平台移植,ic为用户定制. 解决的问题有: 一.build err:主要原因是89和82平台函数和宏定义冲突,如: extern void mt_eint_unmask(unsig ...
- python3----连接字符串数组(join)
join 方法用于连接字符串数组 s = ['a', 'b', 'c', 'd'] print(''.join(s)) print('-'.join(s)) results: abcd a-b-c-d ...
- python3 - 生成器genarator
在Python中,这种一边循环一边计算的机制,称为生成器:generator. 生成器保存的是算法,每次调用 next() ,就计算出 下一个元素的值,直到计算到最后一个元素,没有更多的元素时,抛出 ...