From db38eeada9c2187e45e29f056110eac7499b921d Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 31 Oct 2023 08:00:18 -0400 Subject: [PATCH] One more thought about max sub-array --- leetcode/stuff.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/leetcode/stuff.py b/leetcode/stuff.py index 5164abc..768f0bb 100644 --- a/leetcode/stuff.py +++ b/leetcode/stuff.py @@ -550,6 +550,19 @@ def sum_max_sub_array(nums: list[int]) -> int: 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: accum: list[int] = [nums[0]]