Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions dynamic_programming/max_sub_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,22 @@ def find_max_cross_sum(A, low, mid, high):

def max_sub_array(nums: List[int]) -> int:
"""
Finds the contiguous subarray (can be empty array)
which has the largest sum and return its sum.
Finds the contiguous subarray
which has the largest sum and return its sum.

>>> max_sub_array([-2,1,-3,4,-1,2,1,-5,4])
>>> max_sub_array([-2, 1, -3, 4, -1, 2, 1, -5, 4])
6
An empty (sub)array has sum 0.
>>> max_sub_array([])
0
If all elements are negative, the largest subarray would be the empty array,
having the sum 0.
>>> max_sub_array([-1,-2,-3])
0
>>> max_sub_array([5,-2,-3])
5
>>> max_sub_array([31, −41, 59, 26, −53, 58, 97, −93, −23, 84])
187
"""
best = 0
current = 0
Expand All @@ -64,6 +71,9 @@ def max_sub_array(nums: List[int]) -> int:


if __name__ == "__main__":
"""
A random simulation of this algorithm.
"""
inputs = [10, 100, 1000, 10000, 50000, 100000, 200000, 300000, 400000, 500000]
tim = []
for i in inputs:
Expand Down