forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheAlgorithms#49 from yvonneFtMore/master
add sum of the longest sub array
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|