{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。给一个数组,返回它的最大连续子序列的和
动态规划:
- 定义数组元素的含义:sum[i]从起点到i位置的最大和
- 找出关系式:sum[i] = sum[i - 1] > 0 ? sum[i - 1] + array[i] : array[i];
- 找出初始值:sum[0]=array[0];
public class T_30_FindGreatestSumOfSubArray {
public int FindGreatestSumOfSubArray(int[] array) {
// dp法:
// if (array == null || array.length == 0) {
// return 0;
// }
// int[] dp = new int[array.length];
// int res = array[0];
// dp[0] = array[0];
// for (int i = 1; i < array.length; i++) {
// dp[i] = dp[i - 1] > 0 ? dp[i - 1] + array[i] : array[i];
// res = Math.max(res, dp[i]);
// }
// return res;
//dp空间优化:将dp[i]和dp[i-1]改为dp
if (array == null || array.length == 0) {
return 0;
}
int res = array[0];
int dp = 0;
for (int a : array) {
dp = dp > 0 ? dp + a : a;
res = Math.max(res, dp);
}
return res;
}
}