Last active
February 16, 2017 06:28
-
-
Save DavidPu/26b89a907d33fce71a3f883253b34d8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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