One more thought about max sub-array

This commit is contained in:
Dan Buch 2023-10-31 08:00:18 -04:00
parent cb68a35b1b
commit db38eeada9
Signed by: meatballhat
GPG Key ID: A12F782281063434

View File

@ -550,6 +550,19 @@ def sum_max_sub_array(nums: list[int]) -> int:
return mmax return mmax
def sum_max_sub_array_i(nums: list[int]) -> tuple[int, int]:
mmax_i: int = 0
mmax = last = prev = nums[0]
for i in range(1, len(nums)):
prev = nums[i] + last
last = max(nums[i], prev)
mmax_i = i if last > mmax else mmax_i
mmax = max(mmax, last)
return mmax_i, mmax
def sum_max_sub_array_accum(nums: list[int]) -> int: def sum_max_sub_array_accum(nums: list[int]) -> int:
accum: list[int] = [nums[0]] accum: list[int] = [nums[0]]