Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sum of the longest sub array #49

Merged
merged 1 commit into from
Nov 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions dynamic_programming/longest_sub_array.py
Original file line number Diff line number Diff line change
@@ -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)