Maximum continuous subsequence and

Source: Internet
Author: User

The maximum continuous subsequences and problems are very old interview questions. The best solution is O (N) Complexity. Of course, some of the smaller ones are worth noting. Here we will summarize three common solutions, focusing on the last O (N) solution. Note that in some questions, the maximum continuous subsequences and if they are negative, 0 is returned. In this question, the maximum continuous subsequences and do not return 0. If they are all negative, returns the maximum negative number. The Problem description calculates the maximum continuous subsequence sum in the array. For example, if the given array is A = {1, 3,-2, 4,-5}, the maximum continuous subsequence is 6, that is, 1 + 3 + (-2) + 4 = 6. Solution 1-O (N ^ 2) solution because of the maximum continuous subsequence and the possibility of starting from a position in the array 0 to n-1, We can traverse 0 to n-1 positions, calculates all consecutive subsequences starting from this position and the medium maximum value. The maximum value is obtained. In more detail, it is to calculate the maximum continuous subsequence sum starting from position 0, and the maximum continuous subsequence sum starting from position 1... Until the maximum continuous subsequence sum starting from position N-1 is obtained, and the maximum and middle values of all these continuous subsequences are the answer. 2-O (NlgN) solution this problem can also be solved through the partitioning method, the maximum continuous subsequence and either appear in the left half of the array or in the right half of the array, it either spans the left and right sides. Therefore, the maximum continuous subsequence and can be obtained by finding the maximum values in these three cases. The 3-O (N) solution also has a better solution, which only takes O (N) time. Because the maximum continuous subsequence and only the positions 0 ~ The end of a position in n-1. When traversing the I element, judge whether the continuous subsequence before it and whether it is greater than 0. If it is greater than 0, the maximum continuous subsequence ending with position I is the sum of element I and the continuous subsequence preceding it. Otherwise, the maximum continuous subsequence ending with position I is the sum of element I. Example: Description: The sequence of K integers {N1, N2 ,..., NK}, which can be expressed as {Ni, Ni + 1 ,..., nj}, where 1 <= I <= j <= K. The maximum continuous subsequence is the element and the largest of all consecutive subsequences, such as the given sequence {-2, 11,-4, 13,-5,-2 }, its maximum continuous subsequence is {11,-4, 13}, and its maximum value is 20. Now, you need to output the first and last elements of the subsequence. Input: The test input contains several test cases. Each test case occupies two rows. Row 1st provides a positive integer K (K <10000), and row 2nd provides K integers separated by spaces. When K is 0, the input ends and the case is not processed. Output: For each test case, the first and last elements of the largest and maximum continuous subsequences are output in one row, separated by spaces. If the maximum continuous subsequence is not unique, the smallest sequence numbers I and j are output (for example, 2nd and 3 groups in the input sample ). If all K elements are negative, the maximum value is 0, and the first and last elements of the entire sequence are output. Sample input: 6-2 11-4 13-5-210-10 1 2 3 4-5-23 3 7-2165-8 3 2 5 01103-1-5-23-1 0 -20 sample output: 20 11 1310 1 410 3 510 10 100-1-20 0 0 AC code: [cpp] # define RUN # ifdef RUN/** http://ac.jobdu.com/problem.php? Pid = 1011 */# include <stdio. h> # include <stdlib. h> # include <string. h> # include <assert. h> # include <string> # include <iostream> # include <sstream> # include <map> # include <set> # include <vector> # include <list> # include <cctype> # include <algorithm> # include <utility> # include <math. h> using namespace std; # define MAXN 10001 int buf [MAXN]; // O (n2) void maxsequence (int n) {int max _ = buf [0]; int ma X_start = buf [0]; int max_end = buf [0]; for (int I = 0; I <n; I ++) {int sum _ = 0; for (int j = I; j <n; j ++) {sum _ + = buf [j]; if (sum _> max _) {max _ = sum _; max_start = buf [I]; max_end = buf [j] ;}} printf ("% d \ n", max _, max_start, max_end );} /* calculate the maximum number of three values */int max3 (int I, int j, int k) {int max _ = I; if (j> max _) {max _ = j;} if (k> max _) {max _ = k;} return max _;} int maxsequence2 (int a [], int l, I Nt u) {if (l> u) {return 0;} if (l = u) {return a [0];} int m = (l + u) /2; // calculate the left half of the largest continuous subsequence spanning left and right. int lmax = a [m], lsum = 0; for (int I = m; I> = 0; I --) {lsum + = a [I]; if (lsum> lmax) {lmax = lsum ;}} /* calculate the right half of the largest continuous subsequence spanning left and right */int rmax = a [m + 1], rsum = 0; for (int I = m + 1; I <= u; I ++) {rsum + = a [I]; if (rsum> rmax) {rmax = rsum ;}} return max3 (lmax + rmax, maxsequence2 (a, 0, m), maxsequence2 (a, m + 1, u);} void Maxsequene3 (int a [], int len) {int maxsum, maxhere; maxsum = maxhere = a [0]; // The maximum value for initialization is a [0] int max_start = buf [0]; int max_end = buf [0]; int tmp = buf [0]; for (int I = 1; I <len; I ++) {if (maxhere <0) {maxhere = a [I]; // if the maximum continuous subsequence at the preceding position is less than or equal to 0, the maximum continuous subsequence ending with the current position I is a [I] tmp = a [I];} else {maxhere + = a [I]; // if the maximum consecutive subsequences at the preceding position and greater than 0, the maximum continuous subsequences ending with the current position I are the sum of the two.} if (maxhere> maxsum) {maxsum = maxhere; // maximum update continuity Subsequence and max_start = tmp; max_end = a [I] ;}} printf ("% d \ n", maxsum, max_start, max_end );} int main () {# ifndef ONLINE_JUDGE freopen ("1011.in"," r ", stdin); freopen (" 1011.out", "w", stdout); # endif int n; while (scanf ("% d", & n) = 1 & n! = 0) {memset (buf, 0, sizeof (buf); for (int I = 0; I <n; I ++) {scanf ("% d ", & buf [I]);} // maxsequence (n); www.2cto.com // printf ("% d \ n", maxsequence2 (buf, 0, n-1 )); maxsequenc4( buf, n) ;}# endif still has problems with solution 2, which need to be solved. Solution 1 and 3 smooth AC

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.