1. 问题描述:
2. 核心思想
问题结构分析——递推关系建立——自底向上计算——最优方案追踪
3. 算法伪代码
3. Java代码实现
// maxContinuousSubarrayDP Algorithm
public class maxContinuousSubarrayDP {
public static void main(String[] args){
int[] input = {1,-2,4,5,-2,8,3,-2,6,3,7,-1};
int max = maxContinuousSubarrayDP(input, input.length);
System.out.println("最大值和"+max);
}
private static int maxContinuousSubarrayDP(int[] X, int n){
int[] D = new int[n];
int[] Rec = new int[n];
D[n-1] = X[n-1];
Rec[n-1] = n-1;
for(int i=n-2; i>=0; i--){
if(D[i+1]>0){
D[i] = X[i] + D[i+1];
Rec[i] = Rec[i+1];
}else{
D[i] = X[i];
Rec[i] = i;
}
}
int sMax = D[0];
for(int i=1; i<n; i++){
if(sMax<D[i]){
sMax = D[i];
System.out.println("开始位置下标(从0开始计算)"+(i));
System.out.println("结束位置下标(从0开始计算)"+Rec[i-1]);
}
}
return sMax;
}
}