Skip to main content

Best Time To Buy Sell Stocks

Definition​

Problem of finding the maximum profit from buying and selling stocks at the right times

Practice​

function maxProfit(prices):
if prices is empty:
return 0

min_price = infinity
max_profit = 0

for price in prices:
min_price = min(min_price, price)
potential_profit = price - min_price
max_profit = max(max_profit, potential_profit)

return max_profit