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

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 and a [p? 1] <A [p]> A [p + 1].

For example, the following array:

?

 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

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

You are going on a trip to a range of mountains whose relative heights are represented by array A, as shown in a figure below. You have to choose how to handle flags you should take with you. The goal is to set the maximum number of flags on the peaks, according to certain rules.

Flags can only be set on peaks.What's more,If you take K flags, then the distance between any two flags shocould 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, abve, with n = 12, if you take:

  • Two Flags, you can set them on peaks 1 and 5;
  • Three flags, you 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 flags in this case.

Write a function:

Int solution (int A [], int N );

That, given a non-empty zero-indexed array a of N integers,Returns the maximum number of flags that can be set on the peaks of the array.

For example, the following array:

?

 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 shoshould return 3, as explained abve.

Assume that:

  • N is an integer within the range [1 .. 200,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 );
  • 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
  1. Due to spacing requirements, if there are K flags, (K-1) * k <n, then K <√ n
  2. The next peak location of each peak can be recorded, so the time complexity is O (n + 1 + 2 +... + √ n) = O (N + √ n ^ 2) = O (N)
Pseudocode

1 def nextPeak(A):2   N = len(A)3   peaks = createPeaks(A)4   next = [0] * N5   next[N - 1] = -16   for i in xrange(N - 2, -1, -1):7       if (peaks[i]):8           next[i] = i9       else:10          next[i] = next[i + 1]11  return next

1 def flags(A):2   N = len(A)3   next = nextPeak(A)4   i = 15   result = 06   while (i * i <= N):7       pos = 08       num = 09       while (pos < N and num < i):10          pos = next[pos]11          if (pos == -1):12              break13          num += 114          pos += i15      result = max(result, num)16      i += 117  return result

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.