Skip to content

Instantly share code, notes, and snippets.

@nma
Created June 22, 2019 19:13
Show Gist options
  • Save nma/9f9c62acece6bde493ed3c2af3ba141b to your computer and use it in GitHub Desktop.
Save nma/9f9c62acece6bde493ed3c2af3ba141b to your computer and use it in GitHub Desktop.
Best time to buy and sell stock single transaction
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if len(prices) < 2:
return 0
low_price = prices[0]
max_profit = 0
for i in range(1, len(prices)):
if prices[i] > low_price:
max_profit = max(max_profit, prices[i] - low_price)
else:
low_price = prices[i]
return max_profit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment