Skip to content

Commit

Permalink
Kadane’s algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatsing authored Oct 17, 2020
1 parent 4e65a21 commit 175cb28
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Kadane_Algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Kadanes algorithm is a Dynamic Programming approach to solvethe largest contiguous elements in an arraywith runtime of O(n).
// Kadanes Algorithm, aka Maximum Sum of Subarray, is an interesting algorithm problem that can be used to solve Maximum Subarray Problem.


def max_subarray(A):
max_so_far = A[0]
elements_so_far = [A[0]]

for x in A[1:]:
if x > sum(elements_so_far + [x]):
elements_so_far = [x]
else:
elements_so_far.append(x)

max_so_far = max(max_so_far, sum(elements_so_far))

return max_so_far

0 comments on commit 175cb28

Please sign in to comment.