正规解法
状态转移方程 f(i)=max
var maxSubArray = function(nums) {
let pre = 0, maxAns = nums[0];
nums.forEach((x) => {
pre = Math.max(pre + x, x);
maxAns = Math.max(maxAns, pre);
});
return maxAns;
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/maximum-subarray/solution/zui-da-zi-xu-he-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
蠢蠢的二维dp
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
let memo = Array.from(new Array(nums.length + 1),
x => Array(nums.length).fill(null))
memo[1] = nums
let max = -Infinity
/** @param {number} i start position
@param {number} j length*/
let dp = function(i,j) {
let a = memo[j][i]
if (memo[j][i] === null)
a = memo[j][i] = Math.max(dp(i,j - 1) + nums[i + j - 1],
dp(i + 1,j - 1) + nums[i])
max = Math.max(a,max)
return a
}
dp(0, nums.length)
return max
};
改成递推 + 滚动数组,还是超时了,这其中有一些边界问题,很值得细细品味(比如j从0开始)
/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
let memo1 = Array(nums.length).fill(0)
let memo2 = []
let max = -Infinity
/** @param {number} i start location
@param {number} j subarr.length */
for (let j = 1;j <= nums.length;j++) {
// 5个数,最后一个i应该是,5 - 2 + 1(位序)
// 转换成数组下标就是len - j了
for (let i = 0;i <= nums.length - j;i++)
max = Math.max(memo2[i] = memo1[i] + nums[i + j - 1],max)
memo1 = memo2;memo2 = []
}
return max
};
标签:nums,小学,number,param,length,let,数组,max,数学
From: https://www.cnblogs.com/dou-fu-gan/p/17088870.html