Skip to content

Instantly share code, notes, and snippets.

@eric-vader
Created April 15, 2021 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric-vader/b4a303d9ca2decb3ce2ec988396fe49d to your computer and use it in GitHub Desktop.
Save eric-vader/b4a303d9ca2decb3ce2ec988396fe49d to your computer and use it in GitHub Desktop.
def one_trade_profit(prices):
if len(prices) == 0:
return 0
else:
buy_price = prices[0]
profit = 0
for price in prices[1:]:
buy_price = min(price, buy_price)
profit = max(profit, price - buy_price)
return profit
def profit(prices, n):
max_profit = 0
# Find split
for i in range(n+1):
profit = one_trade_profit(prices[:i]) + one_trade_profit(prices[i:])
max_profit = max(profit, max_profit)
return max_profit
print(profit([10,22,5,75,65,80],6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment