题解 | #连续子数组的最大和#
连续子数组的最大和
http://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484
此题与卖股票相互借鉴,简单的动态规划
function FindGreatestSumOfSubArray(array)
{
// write code here
if (array.length == 0)
return 0
var sum = array[0] //保存每组的和
var maxSum = array[0] //连续子数组最大和
for (var i = 1; i < array.length; i++) {
sum = Math.max(sum + array[i], array[i]);
maxSum = Math.max(sum, maxSum)
}
return maxSum
}
module.exports = {
FindGreatestSumOfSubArray : FindGreatestSumOfSubArray
};