Max SubArray kadane Algorthim

时间:2016-03-14 02:49:47

标签: c++ algorithm kadanes-algorithm

jquery.chosen.j

我想更新最佳开始索引 如果我有一个数组A = { - 1,-2,5,0,1} 最好的开始应该是索引2和最佳结束索引4我得到了最好的结局,但我不知道如何更新最好的开始 这里的最大子数组是= 6(5 + 0 + 1)

1 个答案:

答案 0 :(得分:0)

保持潜在的最佳起始指数

bestStartCandidate = 0;

if (max_ending_here < 0)
    {
        max_ending_here = 0;
        bestStartCandidate = i + 1;       
    }
    if (max_ending_here > max_so_far)
    {
        max_so_far = max_ending_here;
        bestStart = bestStartCandidate;       
        bestEnd = i;
    }