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 ...
随机推荐
- [ci]项目规划-后续
几个方面来写 1,搭建gitlab 配邮箱 域名等使之好用 2,搭建jenkins –yum,安装常见插件 3,搭建sonar,汉化 4,安装sonar-scanner 0,实现sonar ...
- 使用Crypto++库编译出错 解决办法
错误信息: >------ 已启动生成: 项目: testCrypto++, 配置: Debug Win32 ------ >正在编译... >main.cpp >正在链接.. ...
- zookeeper安装笔记
安装zookeeper,将zookeeper上传到三个服务器,保存在/usr/local/zookeeper目录下,解压tar包 tar -xzvf zookeeper-3.4.6.tar.gz 进入 ...
- python如何连接mysql数据库
先花点时间来说说一个程序怎么和数据库进行交互1.和数据库建立连接2.执行sql语句,接收返回值3.关闭数据库连接使用MySQLdb也要遵循上面的几步.让我们一步步的进行. 1.MySQL数据库要用My ...
- python学习---简介
http://www.cnblogs.com/wuguanglei/p/3866583.html http://www.cnblogs.com/wuguanglei/p/3866583.html ok ...
- Android-ViewPagerIndicator框架使用——TabPageIndicator
前言:TabPageIndicator这个类和之前的不大一样,他不仅提供了展示的功能,而且可以点击,下面的viewpager可以跳转的有点tabhost的感觉. 一:布局文件的定义,simple_ta ...
- 解决ionic 2载入速度慢的问题
1.ionic build android --prod 使用生产环境的服务器 (最关键部分) [--production-mode=y,n] ................. Tell Io ...
- 椭圆参数方程中的θ(离心角Theta)
椭圆参数方程中的离心角θ是交以其x轴对应外接圆上点的角度(或是交以其y轴对应内接圆上点的角度) 椭圆的参数程为:x=acosθy=bsinθ.M(x,y)椭圆上一点.过M作直线⊥X轴,交以O为圆心,以 ...
- 去OpenCVManager,大部分为转载,仅当自己学习使用
去OpenCVManager方法,可以参考这篇博客http://blog.csdn.net/yanzi1225627/article/details/27863615,可以用,挺好的.我这里只是做个总 ...
- socket编码问题
server.py import socket ip_port = ('127.0.0.1',9999) sk = socket.socket() sk.bind(ip_port) sk.listen ...