Max sub-array slidey window
This commit is contained in:
parent
efb8f453ae
commit
3580198513
@ -535,3 +535,22 @@ def copy_random_list(
|
|||||||
ordered_copy[i].random = ordered_copy[hash_idx[hash(entry.random)]]
|
ordered_copy[i].random = ordered_copy[hash_idx[hash(entry.random)]]
|
||||||
|
|
||||||
return ordered_copy[0]
|
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
|
||||||
|
@ -444,3 +444,24 @@ def test_trie_busy():
|
|||||||
assert trie.startsWith("rent") is True
|
assert trie.startsWith("rent") is True
|
||||||
assert trie.startsWith("beer") is True
|
assert trie.startsWith("beer") is True
|
||||||
assert trie.startsWith("jam") 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
|
||||||
|
Loading…
Reference in New Issue
Block a user