Skip to main content

121. Best Time to Buy and Sell Stock

分析

是一个 dp 问题 但基础问题阶段有不用dp的解法、后续再dp同类型一把锁

题解

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

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

return ans
};