121. Best Time to Buy and Sell Stock

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.




def maxProfit(self, prices: List[int]):
    max_profit = 0
   
    cur_min = prices[0]
    for price in prices:
        max_profit = max(max_profit, price - cur_min)
        cur_min = min(cur_min, price)
       
    return max_profit

Comments

Popular posts from this blog

849. Maximize Distance to Closest Person

347. Top K Frequent Elements

139. Word Break