Exercises on codility (13)

Source: Internet
Author: User

(1) absdistinct

Given an array of non-empty integers sorted in a non-descending order, ask how many different absolute values are in the right.

Data range: integer array length [1..10^5], Integer range [-2147483648, +2147483647].

Complexity of requirements: Time O (N), Space O (1)

Analysis: The problem is not difficult ... But the details are important. Because the integer directly takes the absolute value back to the overflow (for example-2147483648), and we do not have extra space hash. So a good way to do this is to merge two sequential sequences in a similar fashion. We start with the smallest negative number and the largest positive number like merge sort. In this way, positive negative numbers are traversed in the order in which the absolute values are gradually reduced. We think of the absolute value of positive and negative numbers as two descending sequence, and then the idea of merging sort, every time a big move on it, until a list is empty, we need to put another list to be counted in. The point is that you can use the X + y symbol instead of the absolute value, because a positive + negative number does not overflow ....


Code:

You can use includes, for example://#include <algorithm>//you can write to stdout for debugging purposes, e.g./ /cout << "This was a debug message" << Endl;int solution (vector<int> &a) {//write your code in    c++11 int answer = 0; for (int i = 0, j = a.size ()-1;i <= J;) {if ((A[i] >= 0) | | (A[j] <= 0)) {for (; I <= j;++answer) {for (int k = i; (i <= J) && (a[i] = = A[k]);            ++i);            }} else {++answer;            int temp = A[i] + a[j]; if (Temp < 0) {for (int k = i; (i <= J) && (a[i] = = A[k]);            ++i); } else if (Temp > 0) {for (int k = j; (i <= J) && (a[j] = = A[k]);            --J); } else {for (int k = i; (i <= J) && (a[i] = = A[k]);                ++i); FoR (int k = j; (i <= J) && (a[j] = = A[k]);            --J);                }}} return answer; }

(2) Counttriangles

Given positive integer array A, length N, subscript starting from 0 (p,q,r), satisfying 0<=p<q<r<n and a[p] + a[q] > A[r], a[q] + a[r] > A[p], a[p] + a[r] > A [Q] The number of triples.

Data range N [0..1000], array element [1..10^9].

Required complexity time O (N ^ 2), Space O (1).

Analysis: Obviously we can't enumerate ... We can sort the array O (NLOGN), even O (n^2). Then the enumeration, just enumerate two smaller sides a[x], a[y], and then we consider the maximum edge a[z], assuming that we fixed x, when y becomes larger a[x] + a[y] Also become larger, we need a[x] + a[y] > A[z], and the z values before y becomes large still satisfy the bar , so we can continue the loop as long as we have the largest z that satisfies the condition last time. So for the same x, the Y and z changes are O (N). Total complexity O (n^2).

Code:

You can also with includes, for example://#include <algorithm> #include <algorithm>int solution (vector<i Nt> &a) {    //write your code in c++98      sort (a.begin (), A.end ());      int n = a.size (), result = 0;      for (int x = 0; x < n; ++x) {        int z = 0;        for (int y = x + 1; y < n; ++y) {             for (z = max (z, y + 1); (Z < N) && (A[x] + a[y] > A[z]); ++z)             ;             Result + = z-y-1;       }        }    return result;}


(3) Given a positive integer non-negative integer array A, and a nonnegative integer m,a all elements in the [0..M], how many non-empty empty arrays (contiguous segments), does not contain duplicate numbers. If the result exceeds 10^9, the 10^9 is returned

Data range: M [0..10^5], array length n [1..10^5]

Requires a complexity of time O (N), Space O (M).

Analysis: This problem is very much like a substring containing only one character, but the difference is that we are asking for the number. We use O (M) space to maintain a sliding window where the values are not duplicated. For each I, we find the J satisfies the window [I.. J] elements are not duplicated, then the number of non-repeating sub-arrays that begin with I is obviously j-i + 1, and then when we slide i + 1, [i + 1..J] obviously have no repetition characters, so J doesn't have to recalculate, then it's OK.

Code:

You can also with includes, for example://#include <algorithm>int solution (int M, vector<int> &a) {
   
    //Write your code in c++98    int j = 0, result = 0;    vector<bool>;    Have.resize (M + 1, false);    for (int i = 0; i < a.size (); ++i) {for        (; (J < A.size ()) && (!have[a[j]]); ++J) {            Have[a[j]] = true;        }        if (result + = j-i) >= 1000000000) {            return 1000000000;        }        Have[a[i]] = false;            }    return result;}
   

(4) Given an array of integers, the sum of two numbers (which can be the same number) satisfies the minimum of absolute values. Returns the absolute value.

Data range: Array length [1..10^5], array element [ -10^9, +10^9].

Complexity required: Time O (Nlogn), Space O (1).


Analysis: and (1) Almost, first sort, then sweep on both sides, similar to the 2-sum, the large absolute value of the pointer move a bit.

Code:

You can also with includes, for example://#include <algorithm> #include <algorithm>int ab (int x) {    retur N (x >= 0)? x: (-X);} int f (int x,int y) {    return ab (x + y);} int solution (vector<int> &a) {    //write your code in c++98    vector<int> a = A;    Sort (A.begin (), A.end ());    int answer = f (a[0],a[0]);     for (int i = 0, j = a.size ()-1; I <= J;) {        answer = min (answer, F (a[i], a[j]));        if (AB (a[i) > AB (A[j])) {            ++i;        }        else {            --j;        }            }    return answer;}



Exercises on codility (13)

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.