Skip to content

Instantly share code, notes, and snippets.

@vetional
Created October 8, 2017 04:00
Show Gist options
  • Save vetional/e4c338fb48f088a49ca5a7ff017113cf to your computer and use it in GitHub Desktop.
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
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