Case Description:
Write a PHP function. It is required that the maximal continuity of any n positive and negative integers require the algorithm time complexity to be as low as possible;
For example: Echo getmaxsum (Array ( -2,1,3,9,-4,2,3,5,-3,-4,1,3));//MAX continuous and yes (1,3,9,-4,2,3,5) Add function return 19
The code is as follows:
Algorithm analysis://1, must be an integer sequence//2, if the entire sequence is not full of negative numbers, the first item of the maximum subsequence must be positive,//otherwise the maximum number of subsequence after the sum plus the first item of negative numbers, and certainly not the largest;//3, if the entire sequence is negative, Then the maximum number of sub-sequences is 0;The whole negative sequence is simple, no example $arr=array ( -2,1,3,9,-4,2,3,5,-3,-4,1,3), $thissum =0; $maxsum =0; $start =0;//record the starting subscript of the subsequence $end=0;// Record the end subscript for ($i =0; $i
$maxsum) {//If the current subsequence is greater than and $maxsum= $thissum of the current maximum subsequence,//changes the current maximum subsequence and $end= $i;} else if ($thissum <0) {//If the sum of the current subsequence is less than 0, the next element value is assumed to be the first of the largest subsequence, where the first item of the maximum self-sequence is guaranteed to be positive $thissum=0;//the sequence is not all negative $start=$ i+1;}} $parr =array ($start, $end, $maxsum), List ($start, $end, $maxsum) = $parr;p rint_r ($arr); Echo ' maximal subsequence is: '; for ($i = $start; $i <= $end, $i + +) {echo $arr [$i]. ' ';} Echo '
The and of the largest subsequence of the echo '. $maxsum;?>
The effect is as follows:Array ( [0] =-2 [1] = 1 [2] = 3 [3] = 9 [4] +-4 [5] = 2 [6] = 3
[7] + 5 [8] = 3 [9] = 4 [Ten] = 1
The and of the maximum subsequence is 19
This completes the case request, the idea is very important!
The above describes the difference between a direct free kick and an indirect free kick PHP asks any n positive and negative integers for the largest consecutive and includes the difference between the direct free kick and the indirect free kick, and wants to be helpful to a friend who is interested in PHP tutorials.