Skip to content

Commit 1da1ab0

Browse files
poyeacclauss
authored andcommitted
Improve doctest and comment for maximum sub-array problem (#1503)
* Doctest and comment for maximum sub-array problem More examples and description for max_sub_array.py * Update max_sub_array.py * Update max_sub_array.py * Fix doctest
1 parent 3fc276c commit 1da1ab0

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

dynamic_programming/max_sub_array.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
author : Mayank Kumar Jha (mk9440)
33
"""
44
from typing import List
5-
import time
6-
import matplotlib.pyplot as plt
7-
from random import randint
85

96

107
def find_max_sub_array(A, low, high):
@@ -43,15 +40,23 @@ def find_max_cross_sum(A, low, mid, high):
4340

4441
def max_sub_array(nums: List[int]) -> int:
4542
"""
46-
Finds the contiguous subarray (can be empty array)
47-
which has the largest sum and return its sum.
43+
Finds the contiguous subarray which has the largest sum and return its sum.
4844
49-
>>> max_sub_array([-2,1,-3,4,-1,2,1,-5,4])
45+
>>> max_sub_array([-2, 1, -3, 4, -1, 2, 1, -5, 4])
5046
6
47+
48+
An empty (sub)array has sum 0.
5149
>>> max_sub_array([])
5250
0
53-
>>> max_sub_array([-1,-2,-3])
51+
52+
If all elements are negative, the largest subarray would be the empty array,
53+
having the sum 0.
54+
>>> max_sub_array([-1, -2, -3])
5455
0
56+
>>> max_sub_array([5, -2, -3])
57+
5
58+
>>> max_sub_array([31, -41, 59, 26, -53, 58, 97, -93, -23, 84])
59+
187
5560
"""
5661
best = 0
5762
current = 0
@@ -64,6 +69,12 @@ def max_sub_array(nums: List[int]) -> int:
6469

6570

6671
if __name__ == "__main__":
72+
"""
73+
A random simulation of this algorithm.
74+
"""
75+
import time
76+
import matplotlib.pyplot as plt
77+
from random import randint
6778
inputs = [10, 100, 1000, 10000, 50000, 100000, 200000, 300000, 400000, 500000]
6879
tim = []
6980
for i in inputs:

0 commit comments

Comments
 (0)