相关疑难解决方法(0)

查找具有最大总和的连续子集

def max_sublist(x):
 max1 = 0
 max2 = 0
 result = []
 for i in x:
     max2 = max(0, max2 + i)
     max1 = max(max1, max2)

 print result
Run Code Online (Sandbox Code Playgroud)

我想添加元素直到具有最大总和的元素.如何仅向结果添加其元素.

对于前者 如果x = [4, -1, 5, 6, -13, 2] 那么结果应该是[4, -1, 5, 6]

python algorithm python-2.7

1
推荐指数
1
解决办法
1711
查看次数

标签 统计

algorithm ×1

python ×1

python-2.7 ×1