Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 511 Bytes

121.买卖股票的最佳时机.md

File metadata and controls

22 lines (17 loc) · 511 Bytes

121.买卖股票的最佳时机

解题思路

暴力法

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,低价往右移,可能存在更低价,高价往左移。可能存在更高价