Sliding window max (peeked a bit)
This commit is contained in:
parent
01a189c5ee
commit
cb68a35b1b
1
leetcode/.testdata/max_sub_array0.json
Normal file
1
leetcode/.testdata/max_sub_array0.json
Normal file
File diff suppressed because one or more lines are too long
@ -13,27 +13,14 @@ keywords = []
|
|||||||
authors = [
|
authors = [
|
||||||
{ name = "Dan Buch", email = "dan@meatballhat.com" },
|
{ name = "Dan Buch", email = "dan@meatballhat.com" },
|
||||||
]
|
]
|
||||||
classifiers = [
|
classifiers = []
|
||||||
"Development Status :: 4 - Beta",
|
|
||||||
"Programming Language :: Python",
|
|
||||||
"Programming Language :: Python :: 3.7",
|
|
||||||
"Programming Language :: Python :: 3.8",
|
|
||||||
"Programming Language :: Python :: 3.9",
|
|
||||||
"Programming Language :: Python :: 3.10",
|
|
||||||
"Programming Language :: Python :: 3.11",
|
|
||||||
"Programming Language :: Python :: Implementation :: CPython",
|
|
||||||
"Programming Language :: Python :: Implementation :: PyPy",
|
|
||||||
]
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ipython",
|
"ipython",
|
||||||
"ipdb"
|
"ipdb",
|
||||||
|
"matplotlib",
|
||||||
|
"numpy"
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.urls]
|
|
||||||
Documentation = "https://github.com/unknown/leetcode#readme"
|
|
||||||
Issues = "https://github.com/unknown/leetcode/issues"
|
|
||||||
Source = "https://github.com/unknown/leetcode"
|
|
||||||
|
|
||||||
[tool.hatch.envs.default]
|
[tool.hatch.envs.default]
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"coverage[toml]>=6.5",
|
"coverage[toml]>=6.5",
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import copy
|
import copy
|
||||||
|
import itertools
|
||||||
|
import math
|
||||||
import random
|
import random
|
||||||
import typing
|
import typing
|
||||||
|
|
||||||
@ -538,19 +540,31 @@ def copy_random_list(
|
|||||||
|
|
||||||
|
|
||||||
def sum_max_sub_array(nums: list[int]) -> int:
|
def sum_max_sub_array(nums: list[int]) -> int:
|
||||||
window_size: int = 1
|
mmax = last = prev = nums[0]
|
||||||
begin: int = 0
|
|
||||||
end: int = window_size
|
|
||||||
max_sum: int = sum(nums)
|
|
||||||
|
|
||||||
while end - begin <= len(nums):
|
for i in range(1, len(nums)):
|
||||||
while end <= len(nums):
|
prev = nums[i] + last
|
||||||
max_sum = max(max_sum, sum(nums[begin:end]))
|
last = max(nums[i], prev)
|
||||||
begin += 1
|
mmax = max(mmax, last)
|
||||||
end += 1
|
|
||||||
|
|
||||||
window_size += 1
|
return mmax
|
||||||
begin = 0
|
|
||||||
end = window_size
|
|
||||||
|
|
||||||
return max_sum
|
|
||||||
|
def sum_max_sub_array_accum(nums: list[int]) -> int:
|
||||||
|
accum: list[int] = [nums[0]]
|
||||||
|
|
||||||
|
for i in range(1, len(nums)):
|
||||||
|
prev: int = nums[i] + accum[-1]
|
||||||
|
accum.append(max(nums[i], prev))
|
||||||
|
|
||||||
|
return max(accum)
|
||||||
|
|
||||||
|
|
||||||
|
def accum_sub_array_maxes(nums: list[int]) -> list[int]:
|
||||||
|
accum: list[int] = [nums[0]]
|
||||||
|
|
||||||
|
for i in range(1, len(nums)):
|
||||||
|
prev: int = nums[i] + accum[-1]
|
||||||
|
accum.append(max(nums[i], prev))
|
||||||
|
|
||||||
|
return accum
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import stuff
|
import stuff
|
||||||
@ -465,7 +467,11 @@ def test_trie_busy():
|
|||||||
[-2, 1],
|
[-2, 1],
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
json.load(open(".testdata/max_sub_array0.json")),
|
||||||
|
11081,
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_maximum_subarray(nums: list[int], expected: int):
|
def test_max_sub_array(nums: list[int], expected: int):
|
||||||
assert stuff.sum_max_sub_array(nums) == expected
|
assert stuff.sum_max_sub_array(nums) == expected
|
||||||
|
Loading…
Reference in New Issue
Block a user