function maxProfit(prices: number[]): number {
let minPrice = Infinity
let maxProfit = 0
for (const price of prices) {
minPrice = Math.min(minPrice, price)
const profit = price - minPrice
maxProfit = Math.max(maxProfit, profit)
}
return maxProfit
}时间复杂度:O(n),空间复杂度:O(1)。