Created
June 22, 2019 19:13
-
-
Save nma/9f9c62acece6bde493ed3c2af3ba141b to your computer and use it in GitHub Desktop.
Best time to buy and sell stock single transaction
This file contains hidden or 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
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