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 ...
随机推荐
- Atitit.收银系统模块架构attilax 总结
Atitit.收银系统模块架构attilax 总结 1. 常规收银系统模块结构1 1.1. 商品管理1 1.2. 会员系统1 1.3. 报表系统1 1.4. 会员卡系统1 1.5. 库存管理1 2. ...
- 浅谈一致性hash
相信做过互联网应用的都知道,如何很好的做到横向扩展,其实是个蛮难的话题,缓存可横向扩展,如果采用简单的取模,余数方式的部署,基本是无法做到后期的扩展的,数据迁移及分布都是问题,举个例子: 假设采用取模 ...
- Mysql 5.7.24 解压版安装步骤
1.设置 MYSQL_HOME 变量(在mysql解压根目录下) 例如:C:\Program Files\mysql-5.7.24 2.系统path 变量最后面增加 %MYSQL_HOME%\bin ...
- 分布式服务框架 Zookeeper(二)官方介绍
ZooKeeper:为分布式应用而生的分布式协调服务 ZooKeeper是一个为分布式应用而设计的分布式的.开源的协调服务.它提供了一套简单的原语,分布式应用利用这套原语可以实现更高层的服务,比如一致 ...
- linux内核常用函数或宏
1. simple_strtoul 用于将字符串转换为无符号长整数,第3个参数10意味着转换方式是10进制. ival = simple_strtoul(buffer, NULL, 10); 2. 大 ...
- hdu 4705(树形DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4705 思路:反面考虑,用总的方案数减去A,B,C三点在同一路径上的方案数.于是我们可以确定中间点B,在 ...
- poj 1270(dfs+拓扑排序)
题目链接:http://poj.org/problem?id=1270 思路:就是一简单的dfs+拓扑排序,然后就是按字典序输出所有的情况. http://paste.ubuntu.com/59872 ...
- ios -为什么用WKWebView加载相同的html文本,有时展示的内容却不一样。
如图: 红色框部分是WKWebView,左边的是正常显示情况,右边的异常显示.我是在网页加载完成回调里执行的webview高度自适应内容: // 页面加载完成之后调用 - (void)webVie ...
- NGINX快速部署
NGINX快速部署 #!/bin/bash ############################################################################## ...
- C#中的Form,textBox,Bitmap,PictureBox,Button,WebBrowser
因为今天是用的家里的电脑,用的不是retena屏的mac,因此稍后截图的时候大家看到的图片可能会有一些模糊,还请大家海涵... 兄弟们,我胡汉三又回来啦! 以下開始我们的第一个C#程序.我们之前已经通 ...