The difference between a direct free kick and an indirect free kick PHP asks for any n positive and negative integers inside the largest continuous and

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.