Skip to content

Instantly share code, notes, and snippets.

@NdagiStanley
Last active May 10, 2021 15:53
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 NdagiStanley/972738882ba660c4d370412a65f758c0 to your computer and use it in GitHub Desktop.
Save NdagiStanley/972738882ba660c4d370412a65f758c0 to your computer and use it in GitHub Desktop.
Algorithms in Python

Using Python to run algorithms

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