/*
f[i]: 以第i个元素结尾的连续子数组的和
f[i] = max(num[i], num[i] + num[i - 1]....)
= num[i] + max(0, num[i - 1], num[i - 1] + nums[i -2]...)
f[i - 1] = max(num[i - 1], num[i - 1] + num[i - 2]....)
所以动态转移方程:f[i] = nums[i] + max(0, f[i - 1]);
*/
class Solution {
public int maxSubArray(int[] nums) {
int[] f = new int[nums.length + 1];
Arrays.fill(f, Integer.MIN_VALUE);
for (int i = 1; i <= nums.length; i ++) {
f[i] = nums[i - 1];
if (f[i - 1] > 0) f[i] = Math.max(f[i], f[i - 1] + nums[i - 1]);
}
int res = Integer.MIN_VALUE;
for (int i = 1; i <= nums.length; i ++) res = Math.max(res, f[i]);
return res;
}
}