Codility Maxabsslicesum Solution

Source: Internet
Author: User

1. Title (hard)

A non-empty zero-indexed array A of N integers is given. A pair of integers (P, Q), such that 0≤p≤q < N, is called a slice of array A. Thesum of a slice (P, Q) is the total of a[p] + a[p+1] + ... + a[q].

A min abs slice is a slice whose absolute sum is minimal.

For example, array A such that:

  A[0] = 2  a[1] =-4  a[2] = 6  a[3] = 3  a[4] = 9

Contains the following slices, among others:

  • (0, 1), whose absolute sum = + (−4) | = 2
  • (0, 2), whose absolute sum = + (−4) + 6| = 4
  • (0, 3), whose absolute sum = + (−4) + 6 + (−3) | = 1
  • (1, 3), whose absolute sum = | (−4) + 6 + (−3) | = 1
  • (1, 4), whose absolute sum = | (−4) + 6 + (−3) + 9| = 8
  • (4, 4), whose absolute sum = |9| = 9

Both slices (0, 3) and (1, 3) are min abs slices and their absolute sum equals 1.

Write a function:

int solution (int a[], int N);

That, given a non-empty zero-indexed array a consisting of N integers, returns the absolute sum of min abs slice.

For example, given:

  A[0] = 2  a[1] =-4  a[2] = 6  a[3] = 3  a[4] = 9

The function should return 1, as explained above.

Assume that:

  • N is an integer within the range [1.. 100,000];
  • Each element of within the range [−10,000. ].

Complexity:

  • Expected worst-case time complexity is O (N*log (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.2. Topic analysis

The subject looks very simple.

Slice summation, the classical solution of prefix. However, this requirement is the sum of the absolute. It's a little stiff. Greedy algorithms or worms are not very useful.

A little worried about it. See the answer is in front of, but just can't think out. After 20 minutes, I was a little enlightened.

The first is to see the time complexity of O (N*LOGN). This feeling is to sort ah, but the meta-data sort seems useless. Suddenly realize that you can sort the prefix.

I feel very good in a moment.

You think, prefix any difference between the two, that is this section of slice and, then we are actually looking for this paragraph and the smallest absolute value. After we sort the prefix, the values of the two prefix closest to each other will be contiguous. The difference is to find a minimum value. Let's go through this prefix array sequentially, find the minimum value of all the adjacent items, not the absolute values we're looking for? (because it has been sorted from large to small).

Of course, there are other things to do, such as finding the absolute minimum value of the prefix, and the minimum value of the absolute values of the arrays, and comparing them with the results.

Will inevitably come to the final answer.

3. Figured out the code will be dozens of lines, everyone think it!

Codility Maxabsslicesum Solution

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.