LeetCode & Q53-Maximum Subarray-Easy & 动态规划思路分析
Array
DP
Divide and Conquer
Description:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array
[-2,1,-3,4,-1,2,1,-5,4]
,
the contiguous subarray[4,-1,2,1]
has the largest sum =6
.
这题自己没做出来,对DP算法没有了解过,这次把DP算法简单学习了下,再看这道题就是最基础的对DP的应用(最优化子结构)
Best Solution:
public class Solution {
public int maxSubArray(int[] nums) {
int dp = nums[0];
int max = nums[0];
for (int i = 1; i < nums.length; i++) {
dp = nums[i] + (dp > 0 ? dp : 0);
max = Math.max(max, dp);
}
return max;
}
}
显然,题目寻找的是nums[start]
到nums[end]
的和最大,找出这段子数组即可。
在这道题的解法上,在for
循环里很明显是个动态的、多阶段决策的思想。dp
的确定,是一个递推思想,现在的dp
值由上一个dp
值所决定,由于是找最大和,故dp<0
时,直接舍去dp
当前值,并赋值nums[i]
,这实际上是改变start
值。而max
的确定,是在当前max
和刚得出的dp
取大值,相当于确定了end
。
另一种解法我认为也非常好理解,此处为代码:
public static int maxSubArray(int[] A) {
int maxSoFar=A[0], maxEndingHere=A[0];
for (int i=1;i<A.length;++i){
maxEndingHere= Math.max(maxEndingHere+A[i],A[i]);
maxSoFar=Math.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
算法详解原文:
algorithm that operates on arrays: it starts at the left end (element A[1]) and scans through to the right end (element A[n]), keeping track of the maximum sum subvector seen so far. The maximum is initially A[0]. Suppose we've solved the problem for A[1 .. i - 1]; how can we extend that to A[1 .. i]? The maximum
sum in the first I elements is either the maximum sum in the first i - 1 elements (which we'll call MaxSoFar), or it is that of a subvector that ends in position i (which we'll call MaxEndingHere).MaxEndingHere is either A[i] plus the previous MaxEndingHere, or just A[i], whichever is larger.
笔者翻译如下:
算法作用于数组:从左边(元素
A[1]
)开始,向右边(直到A[n]
)扫描,找到最大和。最大值初始化为A[0]
,假设我们现在已经计算到A[1 .. i - 1]
,怎么扩展到A[1 .. i]
呢?前i
个元素的最大和应该是前i-1
个元素中已算出的最大和(我们称为MaxSoFar
),或者是到当前位置i
结束算出的新的和(称为MaxEndingHere
)。其中,MaxEndingHere
是A[i]
加之前的MaxEndingHere
和A[i]
中较大的那一个。
可以看出两种算法思路一致,MaxEndingHere
就相当于是dp
,MaxSoFar
也就是求出的max
。