[Codility] Maxdoubleslicesum

Source: Internet
Author: User



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 a 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 is, 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 should return, 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 within the range [−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.


Copyright 2009–2015 by Codility Limited. All rights Reserved. Unauthorized copying, publication or disclosure prohibited.


Scan the array from the left and right, record the maximum contiguous interval from the left and right to the current element, and then traverse the array again to find Left[i]+right[i], and don't forget to subtract the current element, and note that the first and last element are set to 0.


 
 
 1 // you can use includes, for example:
 2 #include <algorithm>
 3 
 4 // you can write to stdout for debugging purposes, e.g.
 5 // cout << "this is a debug message" << endl;
 6 
 7 int solution(vector<int> &A) {
 8     // write your code in C++11
 9     if (A.size() <= 3) return 0;
10     vector<int> left(A), right(A);
11     int n = A.size();
12     left[0] = left[n-1] = 0;
13     right[0] = right[n-1] = 0;
14     for (int i = 1; i < n - 1; ++i) {
15         left[i] = max(left[i], left[i] + left[i-1]);
16         right[n-1-i] = max(right[n-1-i], right[n-1-i] + right[n-i]);
17     }
18     int res = 0;
19     for (int i = 1; i < n - 1; ++i) {
20         res = max(res, left[i] + right[i] - 2 * A[i]);
21     }
22     return res;
23 }





[Codility] Maxdoubleslicesum


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.