121 Best Time to Buy and Sell Stocks

This commit is contained in:
2024-06-29 10:18:18 -04:00
parent d8665c4e6a
commit 059776458a
@@ -0,0 +1,14 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
max_profit = 0
buy_index = 0
if len(prices) == 1:
return 0
for i, cur_price in enumerate(prices):
profit = cur_price - prices[buy_index]
if profit > max_profit:
max_profit = profit
elif cur_price < prices[buy_index]:
buy_index = i
return max_profit