package com.ytx.dp;
/** 题目描述:给定一个整数数组,如{-1,3,6,0,-9,2,-5,-1,9,3,-3},求该数组中的和最大的连续子序列
*
* @author yuantian xin
*
* 以下是根据之前dp算法的进一步优化,优化空间,并不需要一个数组来打表,可以用两个变量即可。
*/
public class Maximum_Subarray {
public int maxSubArray(int[] A) {
int len = A.length;
int max = Integer.MIN_VALUE;
int temp = Integer.MIN_VALUE;
for (int i=0; i<len; ++i){
if (temp < 0)
temp = A[i];
else{
temp += A[i];
}
if (temp > max){
max = temp;
}
}
return max;
}
public static void main(String[] args) {
int[] data = {1,-2,0,0,3,10,-4,7,2,-5,3};
int max = new Maximum_Subarray().maxSubArray(data);
System.out.println(max);
}
}