def max_profit(prices): if not prices: return 0 min_price = prices[0] max_profit = 0 for price in prices: min_price = min(min_price, price) max_profit = max(max_profit, price - min_price) return max_profit prices = [7, 1, 5, 3, 6, 4] print(max_profit(prices)) # 输出应该是 5,因为最佳买入时间是第2天(价格是1),最佳卖出时间是第5天(价格是6)  

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务