注意 不好使用dp数组来保存每一个状态
let nums = [-2, -1, -10, 4, -1, 2, 1, -5, 4]
let pre = 0, maxAns = nums[0];
nums.forEach((x) => {
maxAns = Math.max(maxAns, pre = Math.max(pre + x, x));
});
console.log(maxAns)
let nums = [-2, -1, -10, 4, -1, 2, 1, -5, 4]
let pre = 0, maxAns = nums[0];
nums.forEach((x) => {
maxAns = Math.max(maxAns, pre = Math.max(pre + x, x));
});
console.log(maxAns)