Maximum subsequence sum (4 methods)

Source: Internet
Author: User
Maximum subsequence sum (4 methods) I. problem description

Enter a group of integers to obtain the subsequence of the numbers and the medium and maximum values. That is, as long as the sum of the largest subsequences is obtained, the largest sequence does not need to be obtained. For example:

  1. Sequence:-2 11-4 13-5-2, then the maximum subsequence is 20.
  2. Sequence:-6 2 4-7 5 3 2-1 6-9 10-2, the maximum subsequence is 16.
II. PHP code implementation

Implementation can refer to: http://www.cnblogs.com/CCBB/archive/2009/04/25/1443455.html

 = $ End) {$ max_value = isset ($ data [$ start])? $ Data [$ start]: 0;} else {$ mid = floor ($ start + $ end)/2); list ($ max_left_value, $ max_left_start, $ max_left_end) = findMax3 ($ data, $ start, $ mid); list ($ max_right_value, $ max_right_start, $ max_right_end) = findMax3 ($ data, $ mid + 1, $ end ); // calculate the maximum value on the left $ max_this_value = $ data [$ mid]; $ max_this_start = $ mid; $ max_this_end = $ mid; $ temp = 0; for ($ I = $ max_this_start; $ I >=$ start; $ I --) {$ temp + = $ data [$ I]; if ($ temp> $ max_this_value) {$ max_this_value = $ temp; $ max_this_start = $ I ;}// calculate the maximum value on the right $ temp = $ max_this_value; for ($ I = $ max_this_end + 1; $ I <= $ end; $ I ++) {$ temp + = $ data [$ I]; if ($ temp> $ max_this_value) {$ max_this_value = $ temp; $ max_this_end = $ I ;}$ max_value = $ max_this_value; $ start = $ max_this_start; $ end = $ max_this_end; if ($ max_value <$ max_left_value) {$ max_value = $ max_left_value; $ start = $ max_left_start; $ end = $ max_left_end;} if ($ max_value <$ max_right_value) {$ max_value = $ max_right_value; $ start = $ max_right_start; $ end = $ max_right_end;} return array ($ max_value, $ start, $ end);} // method 4. algorithm O (N) function findMax4 ($ data) {$ max_value = 0; $ thisSum = 0; $ data_len = count ($ data); $ start = 0; $ end = 0; for ($ I = 0; $ I <$ data_len; $ I ++) {$ thisSum + = $ data [$ I]; if ($ thisSum> $ max_value) {$ max_value = $ thisSum; $ end = $ I;} else if ($ thisSum <0) {$ thisSum = 0; $ start = $ I + 1 ;}} return array ($ max_value, $ start, $ end );}
III. test
 IV. test results
 
  1. N = 1000

    Test result: n = 1000

  2. N = 5000

The first solution is too slow during testing.

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.