Created
October 8, 2017 04:00
-
-
Save vetional/e4c338fb48f088a49ca5a7ff017113cf to your computer and use it in GitHub Desktop.
Max profit 1D wine problem (DP) created by vetional - https://repl.it/MQlJ/0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def maxprofit(price, year, memo): | |
print price, year | |
profit = 0 | |
if len(price) == 1: | |
return price[0] * year | |
key = '-'.join([str(p) for p in price]) + '--' +str(year) | |
if key in memo: | |
print 'memo hit: ' + key | |
return memo[key] | |
if len(price) == 0: | |
return 0 | |
else: | |
profit += max(price[0] * year + maxprofit(price[1:], year + 1, memo), \ | |
price[-1] * year + maxprofit(price[:-1], year + 1, memo)) | |
memo[key] = profit | |
return profit | |
print maxprofit([2,3,5,1,4],1,{}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment