Codility Flags Solution

Source: Internet
Author: User

How to solve the hard issue

1. Problem:

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

A Peak is an array element which are larger than its neighbours. More precisely, it's an index p such that 0 < P < n−1 and a[p−1] < A[p] > a[p + 1].

For example, the following array A:

    A[0] = 1    a[1] = 5    a[2] = 3    a[3] = 4    a[4] = 3    a[5] = 4    a[6] = 1    a[7] = 2    a[8] = 3    a[9 ] = 4    a[10] = 6    a[11] = 2

Have exactly four peaks:elements 1, 3, 5 and 10.

You is going on a trips to a range of mountains whose relative heights is represented by array A, as shown Low. You have the Choose how many the flags you should take with you. The goal is to set the maximum number of the flags in the peaks, according to certain rules.

Flags can only is set on peaks. What's more, if you take K-flags, then the distance between any of the should be greater than or equal to K. The distance between indices P and Q is the absolute value | p−q|.

For example, given the mountain range represented by array A, above, with N = A, if you take:

  • Both flags, you can set them on peaks 1 and 5;
  • Three flags, can set them on peaks 1, 5 and 10;
  • Four flags, you can set only three flags, on peaks 1, 5 and 10.

You can therefore set a maximum of three.

Write a function:

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

That is, given a non-empty zero-indexed array A of N integers, returns the maximum number of flags that can is set on the pea KS of the array.

For example, the following array A:

    A[0] = 1    a[1] = 5    a[2] = 3    a[3] = 4    a[4] = 3    a[5] = 4    a[6] = 1    a[7] = 2    a[8] = 3    a[9] = 4    a[10] = 6    a[11] = 2

The function should return 3, as explained above.

Assume that:

  • N is an integer within the range [1.. 200,000];
  • Each element of within the range [0. 1,000,000,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.2. Dig into the This issue initially I thought the problem was very complicated and needed to involve, for example, the minimum distance between two points, the deletion of nodes, the sorting of the spacing between peak and so on.     But when I saw that the complexity of the time required O (N), I thought I thought more.    Mathematically we can conclude that if dis = the distance between the last peak and the first peak, then (k-1) *k<dis;     So we can figure out the maximum possible k, and the relationship between K and dis, so if the scale of k in accordance with the cycle, then we have succeeded in reducing the time complexity of the operation. For each k, the easiest way to do this is to look from the leftmost peak to the right (because if the dis is reduced from the next start, the K will become less).         So let's start with the left peak, add a K, get the next peak, look down at this point, search down in turn, until the peak number is equal to K, complete, or can't find the next peak (K is too large), then k-1, repeat the upper operation.     This operation also has a technical obstacle to the complexity of time, that is, to find the next peak in I position requires an O (N) level operation, how can we turn it into an O (1) level operation?     You can use an array. We'll first go through this a to find out where all the peak is.     Then define an array nextpeak[], for Nextpeak[i], representing the first peak to be found from the I position (including I position).      With O (N) space, we can reduce the nextpeak operation to the O (1) level. This way, we can always find the K solution through our cyclic algorithm. And with specific validation, the time complexity of the algorithm is O (N) level. 3. Results: 4. The source code is:
//You can write to stdout for debugging purposes, e.g.//printf ("This is a debug message\n");intSolution (intA[],intN) {//Write your code in C99    inti =1; //whether each node is a peak    intIspeak[n]; ispeak[0]=0; Ispeak[n-1]=0; //Peak Number    intCount =0;  for(i=1; i<n-1; i++)    {        if(a[i]>a[i-1]&&a[i]>a[i+1]) {Ispeak[i]=1; Count++; }            Else{Ispeak[i]=0; }    }    //If peak is 0, then exit directly, no discussion.    if(Count = =0)    {        return 0; }    //into the corresponding peak position.     intPeak[count]; intj=0;  for(i=0; i<n;i++)    {        if(ispeak[i]==1) {Peak[j]=i; J++; }    }        intdis = peak[count-1]-peak[0]; //Maximum possible k    intMAXK =1;  while((maxk-1) *maxk<dis) {MAXK++; }    if((maxk-1) *maxk!=dis) maxk--; //deposit at the position of the next peak at the I node, if there is no next peak, 1;    intNextpeak[n]; J=count-1; inttemp =-1;  for(i=n-1;i>0; i--)    {        if(i>Peak[j]) {Nextpeak[i]=temp; }        Else{Temp=Peak[j]; J--; Nextpeak[i]=temp; }        //printf ("%d", Nextpeak[i]);    }            //from MAXK, search down until you find an I (k) to meet the conditions    intStart = peak[0]; intnodes =1;  for(i=maxk;i>0; i--)    {         while(nodes<i) {start= start+i; if(Start > N-1)            {                 Break; } Start=Nextpeak[start]; //printf ("\n%d", start);            if(Start = =-1)            {                 Break; }            Else{nodes++; }        }        if(Nodes = =i) {returni; }        Else{nodes=1; Start= peak[0]; }    }            returnnodes;}

Codility Flags 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.