forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_subarray.py
More file actions
33 lines (24 loc) · 789 Bytes
/
max_subarray.py
File metadata and controls
33 lines (24 loc) · 789 Bytes
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
"""
Maximum Subarray (Kadane's Algorithm)
Find the contiguous subarray with the largest sum.
Reference: https://en.wikipedia.org/wiki/Maximum_subarray_problem
Complexity:
Time: O(n)
Space: O(1)
"""
from __future__ import annotations
def max_subarray(array: list[int]) -> int:
"""Find the maximum sum of a contiguous subarray using Kadane's algorithm.
Args:
array: List of integers (containing at least one number).
Returns:
Largest sum among all contiguous subarrays.
Examples:
>>> max_subarray([1, 2, -3, 4, 5, -7, 23])
25
"""
max_so_far = max_now = array[0]
for i in range(1, len(array)):
max_now = max(array[i], max_now + array[i])
max_so_far = max(max_so_far, max_now)
return max_so_far