Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def better_max_sum(array):
- max_sum = 0
- max_ending_at_current_index = 0
- for value in array:
- max_ending_at_current_index += value
- max_sum = max(max_sum, max_ending_at_current_index)
- max_ending_at_current_index = max(max_ending_at_current_index, 0)
- return max_sum
- def max_sum(array):
- array_of_arrays = []
- for i in range(len(array)):
- for j in range(i, len(array)):
- array_of_arrays.append(array[i:j+1])
- return max(list(map(lambda x: sum(x), array_of_arrays)))
- if __name__ == '__main__':
- tests = [
- [1, 3, -2, 3, 6],
- [1, -1, -2, 5],
- [1, 1, -1, 5],
- [2, 1, -3, 4, -1, 2, 1, -5, 4],
- [-100000, 1, -3, 4, 2, 1, -5, 4],
- [2, 1, -3, 4, -1, 2, 1, -5, -10000]
- ]
- correct_answers = [11, 5, 6, 6, 7, 6]
- answers = list(map(better_max_sum, tests))
- assert answers == correct_answers
- print(answers)
Add Comment
Please, Sign In to add comment