Consider the Kadane's algorithm for finding maximum subarray within an array:
def max_subarray(numbers):
"""Find the largest sum of any contiguous subarray."""
best_sum = 0
current_sum = 0
for x in numbers:
current_sum = max(0, current_sum + x)
best_sum = max(best_sum, current_sum)
return best_sum
The algorithm requires constant space to execute, apparently. But still, it accepts a list of n
elements as input. So is its space complexity O(n)
or O(1)
?
O(1)
. $\endgroup$random.shuffle
), this argument for not counting it breaks down. $\endgroup$