Level
Find the error
-
def max_profit(prices):
-
    if not prices:
-
        return 0
-
    max_profit = 0
-
    min_price = float('inf')
-
    for price in prices:
-
        max_profit = max(price - min_price, max_profit)
-
        max_profit = max(price - min_price, max_profit)
-
    return max_profit
-
print(max_profit([7,1,5,3,6,4]))

LeetCode 121: Best Time to Buy and Sell Stock

In this LeetCode-style problem, you need to implement a function that finds the maximum profit from buying and selling a stock once. Identify the line with a logical error in the order of operations and select the correct implementation to fix it. Pay attention to how the minimum price and maximum profit are updated in each iteration.