121. Best Time to Buy and Sell Stock
Easy
😇 Solution
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices:
return 0
min_price = max(prices)
max_profit = 0
for i in range(len(prices)):
if prices[i] < min_price:
min_price = prices[i]
profit = prices[i] - min_price
if profit > max_profit:
max_profit = profit
return max_profitclass Solution:
def maxProfit(self, prices: List[int]) -> int:
max_profit = 0
for i in range(len(prices)):
for j in range(i+1,len(prices)):
profit = prices[j] - prices[i]
if profit > max_profit:
max_profit = profit
return max_profitLast updated