连续子数组的最大和

对于数组array,从array[1]开始逐个进行相加,与最大值比较,并不停地更替最大值。

连续子数组的最大和

public class Solution {
    public int FindGreatestSumOfSubArray(int[] array) {
        if(array.length == 0)
            return 0;
        int total = array[0];
        int maxSum = array[0];
        for(int i = 1; i < array.length; i++){
            if(total >= 0)
                total += array[i];
            else
                total = array[i];
            if(total > maxSum)
                maxSum = total;
        }
        return maxSum;
    }
}
  • 动态规划法需要学习,用动态规划法如何解决这个问题 ?