PHP implements the maximum number of positive and negative arrays, which is required for the array, which consists of positive and negative numbers, to find out the maximum number of sub-arrays of contiguous elements in the array.
It's actually a backpack variant.
Copy the Code code as follows:
$list = Array (1,-3,-5,-7,8,9,-11,5);
$cur = 0;
$term = 0;
$res = 0;
$begin = 0;
foreach ($list as $k = = $v) {
$cur + = $v;
if ($cur < 0) {
$cur = 0;
$begin = $k + 1;
}
if ($cur > $res) {
$res = $cur;
$term = $k;
}
}
$max _seq = Array_slice ($list, $begin, ($term-$begin) + 1);
Echo $res. ',';
Print_r ($max _seq);
17,array ([0] = 8 [1] = 9)
http://www.bkjia.com/PHPjc/752919.html www.bkjia.com true http://www.bkjia.com/PHPjc/752919.html techarticle PHP implements the maximum number of positive and negative arrays, which is required for the array, which consists of positive and negative numbers, to find out the maximum number of sub-arrays of contiguous elements in the array. This is actually a back ...