From ac5d2354ba6f4a385b32960a3179707aaff69639 Mon Sep 17 00:00:00 2001 From: Yvonne Date: Tue, 29 Nov 2016 13:18:47 +0800 Subject: [PATCH] add sum of the longest sub array --- dynamic_programming/longest_sub_array.py | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 dynamic_programming/longest_sub_array.py diff --git a/dynamic_programming/longest_sub_array.py b/dynamic_programming/longest_sub_array.py new file mode 100644 index 000000000000..b9a5e8de7dad --- /dev/null +++ b/dynamic_programming/longest_sub_array.py @@ -0,0 +1,32 @@ +''' +Auther : Yvonne + +This is a pure Python implementation of Dynamic Programming solution to the edit distance problem. + +The problem is : +Given an array, to find the longest and continuous sub array and get the max sum of the sub array in the given array. +''' + + +class SubArray: + + def __init__(self, arr): + # we need a list not a string, so do something to change the type + self.array = arr.split(',') + print("the input array is:", self.array) + + def solve_sub_array(self): + rear = [int(self.array[0])]*len(self.array) + sum_value = [int(self.array[0])]*len(self.array) + for i in range(1, len(self.array)): + 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] + + +if __name__ == '__main__': + whole_array = input("please input some numbers:") + array = SubArray(whole_array) + re = array.solve_sub_array() + print("the results is:", re) +