-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathlongest_sub_array.py
64 lines (47 loc) · 1.33 KB
/
longest_sub_array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Author : Yvonne
This is a pure Python implementation of Dynamic Programming solution to the
longest_sub_array 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.
"""
def longest_sub_array(arr: list):
"""
Find the longest continuous subarray with the maximum sum.
Args:
arr (list): A list of integers.
Returns:
A Integer which is the max subarray sum in the whole array.
Examples:
>>> longest_sub_array([1, 2, 3, 2, 5])
13
>>> longest_sub_array([5, -4, 3, -2, 1])
5
>>> longest_sub_array([1, 2, 3, -2, 5])
9
>>> longest_sub_array([10, 20, -30, 40, 50])
90
>>> longest_sub_array([])
0
"""
max_so_far = arr[0]
max_ending_here = arr[0]
max_len = 1
curr_len = 1
for i in range(1, len(arr)):
if max_ending_here < 0:
max_ending_here = arr[i]
curr_len = 1
else:
max_ending_here += arr[i]
curr_len += 1
if max_ending_here > max_so_far:
max_so_far = max_ending_here
max_len = curr_len
elif max_ending_here == max_so_far:
max_len = max(max_len, curr_len)
return max_len
if __name__ == "__main__":
import doctest
doctest.testmod()