SOURCE

// 斐波那契数列
let memo = {}
var Fib = function(n){
    if(n == 1 || n == 2){
        return 1
    }
    if(n === 0){
        return 0
    }
    if(!memo[n])  //如果没有计算过,就计算一次并存下来
     memo[n]=Fib(n-1)+Fib(n-2);
    return memo[n];  //如果计算过了,直接返回之前记忆的计算结果
}
// 测试
// console.log(Fib(19))
// console.log(memo)

var maxProfitIII = function(prices, times){
    let DP = new Array(times + 1); 
    DP.fill(new Array(prices.length).fill(0))
    console.log(DP)
    for(let k = 1; k < times + 1; k++){
        let tmpMax = -prices[0]
        for(let i = 1; i < prices.length; i++){
            DP[k][i] = Math.max(DP[k][i - 1], prices[i] + tmpMax)
            tmpMax = Math.max(tmpMax, DP[k-1][i-1] - prices[i])
        }
    }
    return DP[times][prices.length - 1]
}

console.log(maxProfitIII([2, 4, 1], 2))
console 命令行工具 X clear

                    
>
console