Created
April 15, 2021 09:21
-
-
Save eric-vader/b4a303d9ca2decb3ce2ec988396fe49d to your computer and use it in GitHub Desktop.
This file contains 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 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