暴力法
function maxProfit(prices: number[]): number {
let max = 0
for (let i = 0; i < prices.length - 1; i++) {
for (let j = i + 1; j < prices.length; j++) {
max = Math.max(max, prices[j] - prices[i])
}
}
return max
};
提交后运算超时
双指针
假设左边存在低价left,右边存在高价right,低价往右移,可能存在更低价,高价往左移。可能存在更高价