排除不合理的项(负值), 设定一个标杆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.

  1. #!/usr/bin/python2.7
  2. #File: maxChildSum.py
  3. #Author: lxw
  4. #Time: 2014-08-22
  5. #Usage: Find the max sum of a sub-sequence in a sequence.
  6. #NOTE: @1: first, must be the max sum.
  7. # @2: if more than one sum is max(same max value), choose the longer sequence.
  8.  
  9. def main():
  10. #arr = [-12, 0, -14, -14, -13, -14, -2] #"zero" test.
  11. #arr = [0, -14, -14, -13, -14, -2] #another "zero" test.
  12. arr = [-100, -14, -14, -13, -14, -2] #"all negtive" test.
  13. #arr = [-12, 10, 2, -14, -14, 13, -2] #"longer sequence but not equal sum" test.
  14. #arr = [-12, 0, 10, -14, -14, -2] #"as long as better" test.
  15. #arr = [-12, 0, 10, -14, -14, 3, 7, -2] #"same sum & same length" test.
  16. #arr = [-12, 10, -14, -14, 3, 7, -2] #"same sum but longer sequence" test.
  17.  
  18. index = 0
  19. length = len(arr)
  20. while arr[index] < 0:
  21. index += 1
  22. if index == length:
  23. break
  24.  
  25. if index < length:
  26. sum = -1 #This initial value is important.
  27. start = index
  28. temp_sum = 0 #sum
  29. temp_start = index
  30. end = 0
  31.  
  32. while index < length:
  33. temp_sum += arr[index]
  34. if temp_sum >= 0:
  35. if temp_sum > sum:
  36. sum = temp_sum
  37. start = temp_start
  38. end = index
  39. elif temp_sum == sum:
  40. if start == temp_start:
  41. end = index
  42. elif index - temp_start > end - start:
  43. start = temp_start
  44. end = index
  45. else:
  46. temp_sum = 0
  47. temp_start = index + 1
  48. index += 1
  49. print "max sum:{0:<4}start:{1:<4}end:{2:<4}max length:{3:<4}".format(sum, start, end, end-start+1)
  50. else:
  51. #All the numbers are negative.
  52. print "max sum:{0:<4}start:{1:<4}end:{2:<4}max length:{3:<4}".format(0, 0, 0, 0)
  53.  
  54. if __name__ == '__main__':
  55. main()
  56. else:
  57. print "Being imported as a module."

MAXIMUM SUBSEQUENCE SUM PROBLEM的更多相关文章

  1. Solutions for the Maximum Subsequence Sum Problem

    The maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional ...

  2. Maxmum subsequence sum problem

    We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...

  3. Algorithm for Maximum Subsequence Sum z

    MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...

  4. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  5. PAT1007:Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  6. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  7. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  9. 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 ...

随机推荐

  1. Atitit.attilax的 case list 项目经验 案例列表

    Atitit.attilax的 case list 项目经验 案例列表 1. Atian inputmethod 输入法3 2. Ati desktop engine桌面引擎3 3. Acc资金账户系 ...

  2. 无序列表li横向排列

    一.横向两列方式排列: 在网页中,很多地方都会用到无序列表横向排列的形式,通常的写法都是使得li的css样式设置为:float:left的形式即可,li会依次从最左边开始并列对齐, 例如: HTML中 ...

  3. javascript中实现sleep函数

    function sleep(d){ for(var t = Date.now();Date.now() - t <= d;);}

  4. Python 中,matplotlib绘图无法显示中文的问题

    在python中,默认情况下是无法显示中文的,如下代码: import matplotlib.pyplot as plt # 定义文本框和箭头格式 decisionNode = dict(boxsty ...

  5. object is not an instance of declaring class

    错误原因:invoke方法的时候,应该是类的实例对象,而不是类本身 解决方法:把 PowerMockito.doReturn(index_expect).when(IndexController.cl ...

  6. objc_runtime使用方法的几个简单例子(转)

    1. 给NSObject类动态添加属性h定义部分 [cpp] @interface UIWebView (LoadProgress)  @property (nonatomic, assign) NS ...

  7. Colossal Fibonacci Numbers! UVA 11582 寻找循环节

    /** 题目:Colossal Fibonacci Numbers! UVA 11582 链接:https://vjudge.net/problem/UVA-11582 题意:f[0] = 1, f[ ...

  8. linux无密登录

    ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub bigdata@cor2

  9. 我的第九个java程序--spring和mybatis整合(java project)

    思路:入口程序读spring的配置文件-配置文件注入给程序bean--程序拿到bean以操作对象的手法查出程序 入口程序HelloWorld.java package HelloWorld; impo ...

  10. Python Theano ValueError: y_i value out of bounds

    参考 https://groups.google.com/forum/#!topic/theano-users/tY3fNAPYd9k 这个问题是由于outs的数量没有设置对. 里面写到 “excep ...