2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2020-01-18 12:24:33 +00:00
|
|
|
Author : Yvonne
|
2016-11-29 05:18:47 +00:00
|
|
|
|
2020-06-16 08:09:19 +00:00
|
|
|
This is a pure Python implementation of Dynamic Programming solution to the
|
|
|
|
longest_sub_array problem.
|
2016-11-29 05:18:47 +00:00
|
|
|
|
|
|
|
The problem is :
|
2020-06-16 08:09:19 +00:00
|
|
|
Given an array, to find the longest and continuous sub array and get the max sum of the
|
|
|
|
sub array in the given array.
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2016-11-29 05:18:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SubArray:
|
|
|
|
def __init__(self, arr):
|
|
|
|
# we need a list not a string, so do something to change the type
|
2019-10-05 05:14:13 +00:00
|
|
|
self.array = arr.split(",")
|
2016-11-29 05:18:47 +00:00
|
|
|
|
|
|
|
def solve_sub_array(self):
|
2019-10-05 05:14:13 +00:00
|
|
|
rear = [int(self.array[0])] * len(self.array)
|
|
|
|
sum_value = [int(self.array[0])] * len(self.array)
|
2016-11-29 05:18:47 +00:00
|
|
|
for i in range(1, len(self.array)):
|
2019-10-05 05:14:13 +00:00
|
|
|
sum_value[i] = max(
|
|
|
|
int(self.array[i]) + sum_value[i - 1], int(self.array[i])
|
|
|
|
)
|
|
|
|
rear[i] = max(sum_value[i], rear[i - 1])
|
|
|
|
return rear[len(self.array) - 1]
|
2016-11-29 05:18:47 +00:00
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2016-11-29 05:18:47 +00:00
|
|
|
whole_array = input("please input some numbers:")
|
|
|
|
array = SubArray(whole_array)
|
|
|
|
re = array.solve_sub_array()
|
2017-11-25 09:23:50 +00:00
|
|
|
print(("the results is:", re))
|