编辑代码

// 暴力法
let result = 0;

function dfs(prices, index, length, status, profit) {
    if (length < 2) return;

    result = Math.max(result, profit);
    if (index === length) return result;

    dfs(prices, index + 1, length, status, profit);

    // 表示当天不持有股票
    if (status === 0) {
        // 递归下一步是持有股票的情况
        return dfs(prices, index + 1, length, 1, profit - prices[index]);
    } else {   // 表示当天持有股票
        // 递归下一步不持有股票的情况
        return dfs(prices, index + 1, length, 0, profit + prices[index]);
    }
}

const arr = [1,2,3,4,5];
console.log(dfs(arr, 0, arr.length, 0, result));

// 贪心算法

//