diff --git a/leetcode/stuff.py b/leetcode/stuff.py index 3fc6a18..71761f9 100644 --- a/leetcode/stuff.py +++ b/leetcode/stuff.py @@ -535,3 +535,22 @@ def copy_random_list( ordered_copy[i].random = ordered_copy[hash_idx[hash(entry.random)]] return ordered_copy[0] + + +def sum_max_sub_array(nums: list[int]) -> int: + window_size: int = 1 + begin: int = 0 + end: int = window_size + max_sum: int = sum(nums) + + while end - begin <= len(nums): + while end < len(nums): + max_sum = max(max_sum, sum(nums[begin:end])) + begin += 1 + end += 1 + + window_size += 1 + begin = 0 + end = window_size + + return max_sum diff --git a/leetcode/test_stuff.py b/leetcode/test_stuff.py index 27cb0e3..8a2555d 100644 --- a/leetcode/test_stuff.py +++ b/leetcode/test_stuff.py @@ -444,3 +444,24 @@ def test_trie_busy(): assert trie.startsWith("rent") is True assert trie.startsWith("beer") is True assert trie.startsWith("jam") is True + + +@pytest.mark.parametrize( + ("nums", "expected"), + [ + ( + [-2, 1, -3, 4, -1, 2, 1, -5, 4], + 6, + ), + ( + [1], + 1, + ), + ( + [5, 4, -1, 7, 8], + 23, + ), + ], +) +def test_maximum_subarray(nums: list[int], expected: int): + assert stuff.sum_max_sub_array(nums) == expected