[Problem] [array] [DP] [codility] maxslicesum & maxdoubleslicesum

Source: Internet
Author: User
Maxslicesum

5-7 3 5-2 4-1

The maximum substring of this array is 3 5-2 4

Complexity:

  • Expected worst-case time complexity is O (n );
  • Expected worst-case space complexity is O (1), Beyond Input Storage (not counting the storage required for input arguments ).
Ideas

The key to the O (n) solution is to convert the original optimization problem into a problem with the optimal sub-structure, enumerating the right boundary of the maximum string, then, the maximum value is obtained during the process of solving each subproblem. Assume that the maximum value of the subsequence ending with element I is Max.Ending, the maximum value of the subsequence ending with the I + 1 element is max (0, MaxEnding + A [I + 1])

Code

int solution(const vector<int> &A) {    if(A.size() == 0) return 0;    int max_ending_i = 0;    int max_slice = A[0];    for(int i : A){        max_ending_i = max(i, max_ending_i+i);        if(max_ending_i > max_slice) max_slice = max_ending_i;    }    return max_slice;}

Maxdoubleslicesum

A non-empty zero-indexed array a consisting of n integers is given.

A triplet (x, y, z), such that 0 ≤ x <Y <z <n, is called?Double slice.

The?Sum? Of double slice (x, y, z) is the total of a [x + 1] + A [x + 2] +... + A [y? 1] + A [Y + 1] + A [Y + 2] +... + A [Z? 1].

For example, array a such that:

?

 A[0] = 3 A[1] = 2 A[2] = 6 A[3] = -1 A[4] = 4 A[5] = 5 A[6] = -1 A[7] = 2

Contains the following example double slices:

  • Double slice (0, 3, 6), sum is 2 + 6 + 4 + 5 = 17,
  • Double slice (0, 3, 7), sum is 2 + 6 + 4 + 5? 1 = 16,
  • Double slice (3, 4, 5), sum is 0.

The goal is to find the maximal sum of any double slice.

Write a function:

Int solution (int A [], int N );

That, given a non-empty zero-indexed array a consisting of n integers, returns the maximal sum of any double slice.

For example, given:

?

 A[0] = 3 A[1] = 2 A[2] = 6 A[3] = -1 A[4] = 4 A[5] = 5 A[6] = -1 A[7] = 2

The function shocould return 17, because no double slice of array A has a sum of greater than 17.

Assume that:

  • N is an integer within the range [3 .. 100,000];
  • Each element of array A is an integer within the range [? 10,000 .. 10,000].

Complexity:

  • Expected worst-case time complexity is O (n );
  • Expected worst-case space complexity is O (n), Beyond Input Storage (not counting the storage required for input arguments ).

Elements of input Arrays can be modified.

Ideas
  1. At first glance, this is more advanced than the largest child segment and seems to enumerate the right boundary I and center of the largest string, but in fact, only the enumeration of the right boundary I, right boundary from I-1 to I, the center point can only be one of the center or the I-1. When the center point is K, the left boundary is not optimal, an intermediate point requires the optimal left boundary (this is an embedded maxslicesum problem) when it is a I-1; there is also a situation where only one element of a [I-1] is retained. The overall complexity is still time O (N) and space O (1 ).
  2. Another idea is to enumerate only the middle point K, calculate the maximum field and start with the K-1 and k + 1, and then combine the two, the overall complexity of this method is time O (N) and space O (n ).
Code

Int solution (vector <int> & A) {if (. size () <= 3) return 0; int max_left = 0; // when the center point is the right boundary of the I-1 is I, the maximum value of the Left segment is int max_ending = 0; // when the right boundary is I, the maximum value of the two segments is int center = 1; // the center point int max_slice = 0; For (INT I = 3; I <. size (); I ++) {max_left = max (max_left + A [I-2], a [I-2]); // maxslicesum problem max_ending = max (max_ending + A [I-1], a [I-1], max_left); // maxdoubleslicesum problem if (max_ending = A [I-1]) center = I-2; else if (max_ending = max_left) center = I-1; If (max_ending> max_slice) max_slice = max_ending;} return max_slice;} inline int max (int, int B, int c) {If (B> A) A = B; If (C> A) A = C; return ;}

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.