题目地址: 链接
思路: 遍历时记录最小值,当前值减去历史最小值
ts
function maxProfit(prices: number[]): number {
let ans = 0
let lastMin = prices[0]
for(const price of prices) {
lastMin = Math.min(lastMin, price)
ans = Math.max(ans, price - lastMin)
}
return ans
};