Skip to content

Instantly share code, notes, and snippets.

@kzhaokol
Created October 18, 2013 02:47
Show Gist options
  • Save kzhaokol/7035742 to your computer and use it in GitHub Desktop.
Save kzhaokol/7035742 to your computer and use it in GitHub Desktop.
# Maximum Sum Subsequence
# Runtime: O(n)
# Space: O(1)
def maximum_sub_sequence(_array_of_ints):
array_length = len(_array_of_ints)
current_max = current_sum = 0
current_max_left_index = current_sum_left_index = 0
current_max_right_index = current_sum_right_index = 0
for current_index, current_int in enumerate(_array_of_ints):
current_sum += current_int
current_sum_right_index = current_index + 1
if current_sum < 0:
current_sum = 0
current_sum_left_index = current_index + 1
current_sum_right_index = current_index + 1
continue
if current_sum > current_max:
current_max = current_sum
current_max_left_index = current_sum_left_index
current_max_right_index = current_sum_right_index
continue
return (current_max, _array_of_ints[current_max_left_index:current_max_right_index])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment