之前遇到的一道面试题,现在终于会了。

function FindGreatestSumOfSubArray(array)
{
    let curSum = 0, greateSum = array[0];
    array.forEach((value) => {
        if(curSum < 0) {
            curSum = value;
        } else {
            curSum += value;
        }
        if(curSum > greateSum) {
            greateSum = curSum;
        }
    })
    return greateSum;
}