[Problem] [array] [Prime and composite numbers] [codility] peaks

Source: Internet
Author: User

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

A?Peak? Is an array element which is larger than its neighbors. More precisely, it is an index p Such that 0 <p <n? 1 ,? A [p? 1] <A [p] And a [p]> A [p + 1].

For example, the following array:

?

 A[0] = 1 A[1] = 2 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

Has exactly three peaks: 3, 5, 10.

We wantDivide this array into blocks containing the same number of elements. More precisely, we want to choose a number k that will yield the following blocks:

  • A [0], a [1],..., a [k? 1],
  • A [K], a [k + 1],..., a [2 k? 1],
    ...
  • A [n? K], a [n? K + 1],..., a [n? 1].

What's more,Every block shoshould contain at least one peak. Notice that extreme elements of the blocks (for example a [k? 1] or a [k]) can also be peaks, but only if they have both neighbors (including one in an adjacent blocks ).

The goal is to find the maximum number of blocks into which the array A can be divided.

Array A can be divided into blocks as follows:

  • One block (1, 2, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2). This block contains three peaks.
  • Two blocks (1, 2, 3, 4, 3, 4) and (1, 2, 3, 4, 6, 2). Every block has a peak.
  • Three blocks (1, 2, 3, 4), (3, 4, 1, 2), (3, 4, 6, 2 ). every block has a peak. notice in particle that the first block (1, 2, 3, 4) has a peak at a [3], because a [2] <A [3]> A [4], even though a [4] is in the adjacent block.

However, array a cannot be divided into four blocks, (1, 2, 3), (4, 3, 4), (1, 2, 3) and (4, 6, 2), because the (1, 2, 3) blocks do not contain a peak. notice in particle that the (4, 3, 4) block contains two peaks: A [3] and a [5].

The maximum number of blocks that array A can be divided into is three.

Write a function:

Int solution (vector <int> & );

That, given a non-empty zero-indexed array a consisting of n integers, returns the maximum number of blocks into which a can be divided.

If a cannot be divided into some number of blocks, the function shocould return 0.

For example, given:

?

 A[0] = 1 A[1] = 2 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 shoshould return 3, as explained abve.

Assume that:

  • N is an integer within the range [1 .. 100,000];
  • Each element of array A is an integer within the range [0... 1,000,000,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.

Ideas

The general idea is that we want to divide the sequence into C slices. Each slice must have at least one peak, and the peak is a number larger than the left and right sides (the first and last parts of the original sequence cannot be counted ), find the maximum value of C // codility. The description is really cool. Hello.

  1. Use O (n) to calculate the number of peak sum [] from the beginning to the present, and the coordinate difference d between the farthest two peaks
  2. Calculate the maximum part number C, that is, obtain the minimum part length k. The possible range of the feasible solution k is (D/2, min (d, n/2)], to equalize first n % K = 0, followed by sum [k-1], sum [2 * k-1], sum [3 * k-1],... sum [n-1] Has n/K entries. Therefore, the number of times K in the outer loop is equal to (D/2, D] The number of N approx. (less than D/2 ), the inner layer determines whether N/K operations are required (less than 2n/d ). So the time complexity of step 1 is O (n ).
  3. In programming, you should consider the distance between the first peak and the last peak to the beginning and end, and do not confuse the meaning of k c (it is easy to make a variable name with uppercase letters ).
Code

Int solution (vector <int> & A) {int n =. size (); vector <int> npeaks (n + 1, 0); // npeaks [I] indicates the I-th element (not included) previously, the number of peaks int maxd = 0; // Coordinate Difference Between the farthest two peaks D int last_peak =-1; // process the first peak to the start distance for (INT I = 1; I <N-1; I ++) {if (a [I]> A [I + 1] & A [I]> A [I-1]) {npeaks [I + 1] = npeaks [I] + 1; maxd = max (maxd, I-last_peak); last_peak = I ;} else {npeaks [I + 1] = npeaks [I] ;}} maxd = max (maxd, N-last_peak ); // process the distance from the last peak to the end of npeaks [N] = npeaks [N-1]; If (npeaks [N] <1) return 0; If (maxd> n/2) return 1; for (int K = maxd/2; k <= maxd; k ++) {// slice length if (N % K = 0) {bool isvalid = true; int c = N/K; // Number of slice for (INT I = 1; I <= C; I ++) {If (npeaks [I * k]-npeaks [(I-1) * k] <1) {isvalid = false; break ;}} if (isvalid) return C ;}} cout <"fail" <Endl; For (int K = maxd + 1; k ++) {If (N % K = 0) return N/K; // cannot return k }}

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.