Skip to main content

121. 买卖股票的最佳时机

代码

function maxProfit(prices: number[]): number {
let maxProfit = 0
let minPrice = prices[0]

for (let i = 0; i < prices.length; i++) {
minPrice = Math.min(minPrice, prices[i])
maxProfit = Math.max(prices[i] - minPrice, maxProfit)
}

return maxProfit
}