-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathkadanes_algorithm.py
More file actions
55 lines (41 loc) · 1.22 KB
/
kadanes_algorithm.py
File metadata and controls
55 lines (41 loc) · 1.22 KB
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
"""
Kadane's Algorithm: Given an array of integers, find the contiguous subarray
with the largest sum.
Example:
[-2, 1, -3, 4, -1, 2, 1, -5, 4] --> 6 (subarray [4, -1, 2, 1])
Reference: https://en.wikipedia.org/wiki/Maximum_subarray_problem
"""
def kadanes_algorithm(arr: list[int]) -> int:
"""
Finds the maximum sum of a contiguous subarray using Kadane's Algorithm.
Parameters
----------
arr: list[int], the input list of integers
Returns
-------
int: the maximum subarray sum
>>> kadanes_algorithm([-2, 1, -3, 4, -1, 2, 1, -5, 4])
6
>>> kadanes_algorithm([1])
1
>>> kadanes_algorithm([-1, -2, -3])
-1
>>> kadanes_algorithm([5, 4, -1, 7, 8])
23
>>> kadanes_algorithm([0, 0, 0])
0
>>> kadanes_algorithm([-2, -3, 4, -1, -2, 1, 5, -3])
7
"""
if not arr:
raise ValueError("Array cannot be empty")
max_sum = current_sum = arr[0]
for num in arr[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
if __name__ == "__main__":
import doctest
doctest.testmod()
arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
print(f"Maximum subarray sum: {kadanes_algorithm(arr)}")