121. Best Time to Buy and Sell Stock
Description
Solution
class Solution:
def maxProfit(self, prices: List[int]) -> int:
low_price = prices[0]
profit = 0
# start at 1 since element 0 is used to initialize the low_price
for i in range(1, len(prices)):
if prices[i] <= low_price:
low_price = prices[i]
else:
profit = max(profit, prices[i] - low_price)
return profitLast updated