-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathmaxsubarray_v4.py
64 lines (45 loc) · 1.44 KB
/
maxsubarray_v4.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
# Code Listing #4
"""
Maximum subarray problem - final version
"""
from contextlib import contextmanager
import random
import time
@contextmanager
def timer():
""" Measure real-time execution of a block of code """
try:
start = time.time()
yield
finally:
end = (time.time() - start)*1000
print('time taken=> %.2f ms' % end)
def num_array(size):
""" Return a list of numbers in a fixed random range
of given size """
nums = []
for i in range(size):
nums.append(random.randrange(-25, 30))
return nums
def max_subarray1(sequence):
""" Find sub-sequence in sequence having maximum sum """
# this is the version before the final version for testing purposes
max_sum, max_sub = 0, []
for i in range(len(sequence)):
for j in range(i+1, len(sequence)):
sub_seq = sequence[i:j+1]
sum_s = sum(sub_seq)
if sum_s > max_sum:
max_sum, max_sub = sum_s, sub_seq
return max_sum, max_sub
def max_subarray(sequence):
""" Maximum subarray - optimized version """
max_ending_here = max_so_far = 0
for x in sequence:
max_ending_here = max(0, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
if __name__ == "__main__":
#with timer():
# max_subarray1(num_array(10000))
print(max_subarray([-5, 20, -10, 30, 15]))