The problem is to give an array consisting of positive and negative numbers to find the maximum value of the child array consisting of continuous elements in the array. The following is an example of PHP implementation. if you need a friend, you can refer to php to implement the maximum subsequence of a positive and negative number array. the array is composed of positive and negative numbers, find the maximum value of the child array consisting of continuous elements in the array.
This is actually a bag variant.
The code is 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)