Skip to content

Instantly share code, notes, and snippets.

@cbdavide
Created October 31, 2017 11:52
Show Gist options
  • Save cbdavide/73be7fb0bf6585d34980ea2527e18062 to your computer and use it in GitHub Desktop.
Save cbdavide/73be7fb0bf6585d34980ea2527e18062 to your computer and use it in GitHub Desktop.
Maximum subarray product.py
''''Maximum Subarray Problem - Extended Kadane
Source: https://www.quora.com/How-do-I-solve-maximum-product-subarray-problems
'''
def max_product(A):
maxG, maxC, minC = A[0], A[0], A[0]
for num in A:
temp = maxC
maxC = max([num, temp * num, minC * num])
minC = min([num, temp * num, minC * num])
maxG = max([maxG, maxC, minC])
print(maxG)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment