Python/dynamic_programming/max_sum_contigous_subsequence.py
Taufik Algi F c717f8d860 add max sum contigous subsequence (#1537)
* add max sum contigous subsequence

* fix typo

* Add doctest and type hints

* Create autoblack.yml
2019-10-31 18:45:01 +01:00

21 lines
458 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)
s = [0] * n
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))