A little feed for thought or at least a fun fact:
Timeline of solving the maximum subarray problem
timeline
1977: Problem and solution proposed by Ulf Grenander
: Better performing algorithm proposed by Michael Shamos
1978: .
1979: .
1980: .
1981: .
1982: .
1983: .
1984: Best possible algorithm published by Jay Kadane
| Author | Occupation | Figured out the solution... | Time complexity |
|---|---|---|---|
| Grenander | mathematician, statistician, professor | ||
| Shamos | comp-scientist, mathematician, professor | overnight | |
| Kadane | statistician, professor | in a minute |
The
def max_subarray(arr):
best = float('-inf')
current = 0
for num in arr:
current = max(num, current + num)
best = max(best, current)
return bestObvious, right?