Maximum subarray problem: Difference between revisions

Content deleted Content added
No empty subarrays admitted: suggest to even show the lines modified accordingly
Empty subarrays admitted: introduce line numbers everywhere in code
Line 62:
Thus, the problem can be solved with the following code,{{sfn|Bentley|1989|p=74}}{{sfn|Gries|1982|p=211}} expressed here in [[Python (programming language)|Python]]:
 
<syntaxhighlight lang="python" line="1">
def max_subarray(numbers):
"""Find the largest sum of any contiguous subarray."""
Line 78:
 
This algorithm calculates the maximum subarray ending at each position from the maximum subarray ending at the previous position, so it can be viewed as a trivial case of [[dynamic programming]].
 
===No empty subarrays admitted===
For the variant of the problem which disallows empty subarrays, <code>best_sum</code> should be initialized to negative infinity instead{{sfn|Bentley|1989|p=78,171}}