-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaximum-subarray.py
More file actions
19 lines (16 loc) · 820 Bytes
/
maximum-subarray.py
File metadata and controls
19 lines (16 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
# @param A, a list of integers
# @return an integer
def maxSubArray(self, A):
# this is best solved using Kadane's algorithm
# we need two variables to solve this -
# --- (1) max ending here - this keeps track of the largest subbarray sum till that particular index
# --- (2) max so far - this keeps track of the largest sum of all sub arrays parsed so far.
max_ending_here = A[0]
max_so_far = A[0]
# We initialize both of these variables to A[0] - the first element of the array.
for x in A[1:]:
# starting from the second element , A[1]
max_ending_here = max(x, max_ending_here+x)
max_so_far = max(max_so_far,max_ending_here)
return max_so_far