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的 case list 项目经验 案例列表
Atitit.attilax的 case list 项目经验 案例列表 1. Atian inputmethod 输入法3 2. Ati desktop engine桌面引擎3 3. Acc资金账户系 ...
- 无序列表li横向排列
一.横向两列方式排列: 在网页中,很多地方都会用到无序列表横向排列的形式,通常的写法都是使得li的css样式设置为:float:left的形式即可,li会依次从最左边开始并列对齐, 例如: HTML中 ...
- javascript中实现sleep函数
function sleep(d){ for(var t = Date.now();Date.now() - t <= d;);}
- Python 中,matplotlib绘图无法显示中文的问题
在python中,默认情况下是无法显示中文的,如下代码: import matplotlib.pyplot as plt # 定义文本框和箭头格式 decisionNode = dict(boxsty ...
- object is not an instance of declaring class
错误原因:invoke方法的时候,应该是类的实例对象,而不是类本身 解决方法:把 PowerMockito.doReturn(index_expect).when(IndexController.cl ...
- objc_runtime使用方法的几个简单例子(转)
1. 给NSObject类动态添加属性h定义部分 [cpp] @interface UIWebView (LoadProgress) @property (nonatomic, assign) NS ...
- Colossal Fibonacci Numbers! UVA 11582 寻找循环节
/** 题目:Colossal Fibonacci Numbers! UVA 11582 链接:https://vjudge.net/problem/UVA-11582 题意:f[0] = 1, f[ ...
- linux无密登录
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub bigdata@cor2
- 我的第九个java程序--spring和mybatis整合(java project)
思路:入口程序读spring的配置文件-配置文件注入给程序bean--程序拿到bean以操作对象的手法查出程序 入口程序HelloWorld.java package HelloWorld; impo ...
- Python Theano ValueError: y_i value out of bounds
参考 https://groups.google.com/forum/#!topic/theano-users/tY3fNAPYd9k 这个问题是由于outs的数量没有设置对. 里面写到 “excep ...