Skip to content

Commit ac5d235

Browse files
committed
add sum of the longest sub array
1 parent 3f505c5 commit ac5d235

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Auther : Yvonne
3+
4+
This is a pure Python implementation of Dynamic Programming solution to the edit distance problem.
5+
6+
The problem is :
7+
Given an array, to find the longest and continuous sub array and get the max sum of the sub array in the given array.
8+
'''
9+
10+
11+
class SubArray:
12+
13+
def __init__(self, arr):
14+
# we need a list not a string, so do something to change the type
15+
self.array = arr.split(',')
16+
print("the input array is:", self.array)
17+
18+
def solve_sub_array(self):
19+
rear = [int(self.array[0])]*len(self.array)
20+
sum_value = [int(self.array[0])]*len(self.array)
21+
for i in range(1, len(self.array)):
22+
sum_value[i] = max(int(self.array[i]) + sum_value[i-1], int(self.array[i]))
23+
rear[i] = max(sum_value[i], rear[i-1])
24+
return rear[len(self.array)-1]
25+
26+
27+
if __name__ == '__main__':
28+
whole_array = input("please input some numbers:")
29+
array = SubArray(whole_array)
30+
re = array.solve_sub_array()
31+
print("the results is:", re)
32+

0 commit comments

Comments
 (0)