Skip to content

Instantly share code, notes, and snippets.

@Dimanaux
Created January 25, 2019 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dimanaux/6bdc144588f5e31069c0e0160f296ef8 to your computer and use it in GitHub Desktop.
Save Dimanaux/6bdc144588f5e31069c0e0160f296ef8 to your computer and use it in GitHub Desktop.
Find the subarray (continuous subsequence) with max sum or its sum
def max_subarray_naive(a: list) -> int:
""" takes O(n^2) time
just sum all of the subarrays
"""
m = a[0] if len(a) > 0 else 0
for i in range(len(a)):
for j in range(i, len(a)):
m = max(m, sum(a[i:j]))
return m
def max_subarray_smart(a: list) -> int:
"""
"""
def max_subarray_iter(a: list, begin: int, end: int):
pass
def max_subarray_with_index(a: list, i: int):
pass
return max_subarray_iter(a, 0, len(a))
def max_subarray_dynamic(a: list) -> int:
"""
"""
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment