mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
6fdd53c676
* Fixes redefinition of a variable * Fixes implementing __eq__ * Updates docstring
21 lines
443 B
Python
21 lines
443 B
Python
def max_subarray_sum(nums: list) -> int:
|
|
"""
|
|
>>> max_subarray_sum([6 , 9, -1, 3, -7, -5, 10])
|
|
17
|
|
"""
|
|
if not nums:
|
|
return 0
|
|
n = len(nums)
|
|
|
|
res, s, s_pre = nums[0], nums[0], nums[0]
|
|
for i in range(1, n):
|
|
s = max(nums[i], s_pre + nums[i])
|
|
s_pre = s
|
|
res = max(res, s)
|
|
return res
|
|
|
|
|
|
if __name__ == "__main__":
|
|
nums = [6, 9, -1, 3, -7, -5, 10]
|
|
print(max_subarray_sum(nums))
|