Using Python to run algorithms
-
-
Save NdagiStanley/972738882ba660c4d370412a65f758c0 to your computer and use it in GitHub Desktop.
Algorithms in Python
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 max_profitability(prices): | |
max_profit = -1 | |
initial_value = prices[0] | |
# O(N) - linear time complexity | |
for i in prices[1:]: | |
if initial_value > i: | |
initial_value = i | |
max_profit = (i - initial_value if (i - initial_value) > max_profit else max_profit) | |
return max_profit | |
prices = [10, 20, 30, 40, 50] # 40 | |
prices = [50, 20, 30, 40, 10] # 20 | |
prices = [50, 40, 30, 20, 10] # 0 | |
print(max_profitability(prices)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment