Leetcode 121: Buy and sell stocks

Subject:
Insert picture description here
Code:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices:
            return 0
        
        profit = 0
        buy_stock = prices[0]
        for i in range(len(prices)):
            if buy_stock > prices[i]:
                buy_stock = prices[i]
            profit = max(prices[i] - buy_stock, profit)

        return profit

If you feel good, please like and follow the message~
Thank you for joining us~

Guess you like

Origin blog.csdn.net/BSCHN123/article/details/113831559