Created
January 25, 2019 06:46
-
-
Save Dimanaux/6bdc144588f5e31069c0e0160f296ef8 to your computer and use it in GitHub Desktop.
Find the subarray (continuous subsequence) with max sum or its sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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