数据结构-最大子序列和O(N^3) 可刷新运行 刷新

算法开销 O(N^3)

代码:

$arr=[-2,11,-4,13,-5,-2];
function maxSubsequenceSum($arr,$n){
    $maxSum=0;
    for ($i=0;$i<$n;$i++){
        for ($j=$i;$j<$n;$j++){
            $thisSum=0;
            for ($k=$i;$k<=$j;$k++){
                $thisSum+=$arr[$k];
            }
            if($thisSum>$maxSum){
                $maxSum=$thisSum;
            }
        }
    }
    return $maxSum;
}
echo maxSubsequenceSum($arr,6)."\n";
echo "<img src='https://jc91715.top/storage/app/blog/o7gLvg5nHj.png'/>";

输出:

20
遇到问题?