Skip to content

Instantly share code, notes, and snippets.

@DavidPu
Last active February 16, 2017 06:28
Show Gist options
  • Select an option

  • Save DavidPu/26b89a907d33fce71a3f883253b34d8a to your computer and use it in GitHub Desktop.

Select an option

Save DavidPu/26b89a907d33fce71a3f883253b34d8a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
def maxsum(arr):
# maxsum, start, end:
result = [arr[0], 0, 0]
curr = [0, 0, 0]
for i in range(0, len(arr)):
s = arr[i] + curr[0]
if s > result[0]:
result[0] = s
result[1] = curr[1]
result[2] = i if s >= 0 else curr[2]
if s >= 0:
curr[0] = s
curr[2] = i
else:
curr[0] = 0
curr[1] = curr[2] = i + 1
print('sum:%d,[%d:%d]' % (result[0], result[1], result[2])),
print(arr[result[1]: result[2] + 1])
array = map(int, os.sys.argv[1:])
maxsum(array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment