Skip to content

Latest commit

 

History

History
17 lines (13 loc) · 346 Bytes

122.买卖股票的最佳时机-ii.md

File metadata and controls

17 lines (13 loc) · 346 Bytes

122.买卖股票的最佳时机-ii

解题思路

贪心法

获取每个后面的值比前面的值要大的区间即可

function maxProfit(prices: number[]): number {
    let profile = 0
    for (let i = 0; i < prices.length - 1; i++) {
        profile += Math.max(0, prices[i + 1] - prices[i])
    }
    return profile
};