Search code examples
javascriptalgorithm

Peak and Flag Codility latest chellange


I'm trying to solve the latest codility.com question (just for enhance my skills). I tried allot but not getting more than 30 marks there so now curious what exactly I am missing in my solution.

The question says

A non-empty zero-indexed array A consisting of N integers is given. A peak is an array element which is larger than its neighbours. 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:

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. You have to choose how many 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 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 = 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 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, given the array above

the function should return 3, as explained above.

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); expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

So I tried this code according to my understanding of question

var A = [1,5,3,4,3,4,1,2,3,4,6,2];

function solution(A) {
 array = new Array();  
   for (i = 1; i < A.length - 1; i++) {  
    if (A[i - 1] < A[i] && A[i + 1] < A[i]) {  
     array.push(i);  
    }  
   }  

  //console.log(A);
  //console.log(array);
  var position = array[0];
  var counter = 1;
  var len = array.length;
  for (var i = 0; i < len; i++) {
    if (Math.abs(array[i+1] - position) >= len) {
        position = array[i+1];
        counter ++;
        }
    }

  console.log("total:",counter);
  return counter;

}

The above code works for sample array elements: [1,5,3,4,3,4,1,2,3,4,6,2] Get peaks at indices: [1, 3, 5, 10] and set flags at 1, 5, and 10 (total 3)

But codility.com says it fails on array [7, 10, 4, 5, 7, 4, 6, 1, 4, 3, 3, 7] My code get peaks at indices: [1, 4, 6, 8] and set flags at 1 and 6 (total 2) but coditity.com says it should be 3 flags. (no idea why) Am I miss-understanding the question ?

Please I am only looking for the hint/algo. I know this question is already asked by someone and solved on private chatroom but on that page I tried to get the help with that person but members rather flagging my posts as inappropriate answer so I am asking the question here again.

P.S: You can try coding the challenge yourself here!


Solution

  • Missing 100% PHP solution :)

    function solution($A)
    {
        $p = array(); // peaks
        for ($i=1; $i<count($A)-1; $i++)
            if ($A[$i] > $A[$i-1] && $A[$i] > $A[$i+1])
                $p[] = $i;
    
        $n = count($p);
        if ($n <= 2)
            return $n;
    
        $maxFlags = min(intval(ceil(sqrt(count($A)))), $n); // max number of flags
        $distance = $maxFlags; // required distance between flags
        // try to set max number of flags, then 1 less, etc... (2 flags are already set)
        for ($k = $maxFlags-2; $k > 0; $k--)
        {
            $left = $p[0];
            $right = $p[$n-1];
            $need = $k; // how many more flags we need to set
    
            for ($i = 1; $i<=$n-2; $i++)
            {
                // found one more flag for $distance
                if ($p[$i]-$left >= $distance && $right-$p[$i] >= $distance)
                {
                    if ($need == 1)
                        return $k+2;
                    $need--;
                    $left = $p[$i];
                }
    
                if ($right - $p[$i] <= $need * ($distance+1))
                    break; // impossible to set $need more flags for $distance
            }
    
            if ($need == 0)
                return $k+2;
    
            $distance--;
        }
        return 2;
    }