A non-empty 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] = 2has 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 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:
class Solution { public int solution(int[] A); }
that, given a non-empty 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:
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] = 2the function should return 3, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..400,000];
- each element of array A is an integer within the range [0..1,000,000,000].
using System;
using System.Collections.Generic;
class Solution {
public int solution(int[] A)
{
int[] peaks = FindPeaks(A);
// For now work backwards
//return CountBackwards(peaks);
// Try a binary search
return BinarySearch(peaks);
}
private static int[] FindPeaks(int[] mountains)
{
int numberOfMountains = mountains.Length;
int lastElevation = mountains[0];
bool possiblePeak = false;
var peaks = new List<int>();
// First and last mountains can never be peaks because they can
// never satisfy A[P - 1] < A[P] < A[P + 1] where 0 < P < N - 1
for (int i = 1; i < numberOfMountains - 1; i++)
{
int elevation = mountains[i];
if (elevation > lastElevation)
{
// Last mountain was definitely not a peak but this one could be
lastElevation = elevation;
possiblePeak = true;
continue;
}
if (elevation < lastElevation && possiblePeak)
// Last mountain was a peak
peaks.Add(i - 1);
lastElevation = elevation;
possiblePeak = false;
}
// Second last mountain could still be a peak
if (numberOfMountains > 2 && possiblePeak && mountains[numberOfMountains - 2] > mountains[numberOfMountains - 1])
peaks.Add(numberOfMountains - 2);
return peaks.ToArray();
}
private static int CountBackwards(int[] peaks)
{
int numberOfPeaks = peaks.Length;
int flags = numberOfPeaks;
int placedFlags = CountPlacedFlags(peaks, flags);
while (placedFlags != flags--)
placedFlags = CountPlacedFlags(peaks, flags);
return placedFlags;
}
private static int BinarySearch(int[] peaks)
{
int numberOfPeaks = peaks.Length;
int high = numberOfPeaks;
int low = 0;
int placedFlags = CountPlacedFlags(peaks, high);
if (placedFlags == high)
return placedFlags;
int lastEqualResult = 0;
while (low <= high)
{
int middle = (high - low) / 2 + low;
placedFlags = CountPlacedFlags(peaks, middle);
if (placedFlags == middle)
{
lastEqualResult = placedFlags;
low = middle + 1;
}
else
{
// If we went one higher than our last equal result, then the last equal result is it
if (lastEqualResult > 0 && (middle - lastEqualResult) == 1)
break;
high = middle - 1;
}
}
return lastEqualResult;
}
private static int CountPlacedFlags(int[] peaks, int flags)
{
int numberOfPeaks = peaks.Length;
// First peak always gets a flag
int numberOfPlacedFlags = 1;
int lastPlacedFlag = 0;
for (int i = 1; i < numberOfPeaks; i++)
{
int distance = peaks[i] - peaks[lastPlacedFlag];
if (distance < flags)
continue;
numberOfPlacedFlags++;
lastPlacedFlag = i;
}
return Math.Min(numberOfPlacedFlags, flags);
}
}
using System;
using System.Collections.Generic;
class Solution {
public int solution(int[] A)
{
int[] peaks = FindPeaks(A);
// For now work backwards
//return CountBackwards(peaks);
// Try a binary search
return BinarySearch(peaks);
}
private static int[] FindPeaks(int[] mountains)
{
int numberOfMountains = mountains.Length;
int lastElevation = mountains[0];
bool possiblePeak = false;
var peaks = new List<int>();
// First and last mountains can never be peaks because they can
// never satisfy A[P - 1] < A[P] < A[P + 1] where 0 < P < N - 1
for (int i = 1; i < numberOfMountains - 1; i++)
{
int elevation = mountains[i];
if (elevation > lastElevation)
{
// Last mountain was definitely not a peak but this one could be
lastElevation = elevation;
possiblePeak = true;
continue;
}
if (elevation < lastElevation && possiblePeak)
// Last mountain was a peak
peaks.Add(i - 1);
lastElevation = elevation;
possiblePeak = false;
}
// Second last mountain could still be a peak
if (numberOfMountains > 2 && possiblePeak && mountains[numberOfMountains - 2] > mountains[numberOfMountains - 1])
peaks.Add(numberOfMountains - 2);
return peaks.ToArray();
}
private static int CountBackwards(int[] peaks)
{
int numberOfPeaks = peaks.Length;
int flags = numberOfPeaks;
int placedFlags = CountPlacedFlags(peaks, flags);
while (placedFlags != flags--)
placedFlags = CountPlacedFlags(peaks, flags);
return placedFlags;
}
private static int BinarySearch(int[] peaks)
{
int numberOfPeaks = peaks.Length;
int high = numberOfPeaks;
int low = 0;
int placedFlags = CountPlacedFlags(peaks, high);
if (placedFlags == high)
return placedFlags;
int lastEqualResult = 0;
while (low <= high)
{
int middle = (high - low) / 2 + low;
placedFlags = CountPlacedFlags(peaks, middle);
if (placedFlags == middle)
{
lastEqualResult = placedFlags;
low = middle + 1;
}
else
{
// If we went one higher than our last equal result, then the last equal result is it
if (lastEqualResult > 0 && (middle - lastEqualResult) == 1)
break;
high = middle - 1;
}
}
return lastEqualResult;
}
private static int CountPlacedFlags(int[] peaks, int flags)
{
int numberOfPeaks = peaks.Length;
// First peak always gets a flag
int numberOfPlacedFlags = 1;
int lastPlacedFlag = 0;
for (int i = 1; i < numberOfPeaks; i++)
{
int distance = peaks[i] - peaks[lastPlacedFlag];
if (distance < flags)
continue;
numberOfPlacedFlags++;
lastPlacedFlag = i;
}
return Math.Min(numberOfPlacedFlags, flags);
}
}
using System;
using System.Collections.Generic;
class Solution {
public int solution(int[] A)
{
int[] peaks = FindPeaks(A);
// For now work backwards
//return CountBackwards(peaks);
// Try a binary search
return BinarySearch(peaks);
}
private static int[] FindPeaks(int[] mountains)
{
int numberOfMountains = mountains.Length;
int lastElevation = mountains[0];
bool possiblePeak = false;
var peaks = new List<int>();
// First and last mountains can never be peaks because they can
// never satisfy A[P - 1] < A[P] < A[P + 1] where 0 < P < N - 1
for (int i = 1; i < numberOfMountains - 1; i++)
{
int elevation = mountains[i];
if (elevation > lastElevation)
{
// Last mountain was definitely not a peak but this one could be
lastElevation = elevation;
possiblePeak = true;
continue;
}
if (elevation < lastElevation && possiblePeak)
// Last mountain was a peak
peaks.Add(i - 1);
lastElevation = elevation;
possiblePeak = false;
}
// Second last mountain could still be a peak
if (numberOfMountains > 2 && possiblePeak && mountains[numberOfMountains - 2] > mountains[numberOfMountains - 1])
peaks.Add(numberOfMountains - 2);
return peaks.ToArray();
}
private static int CountBackwards(int[] peaks)
{
int numberOfPeaks = peaks.Length;
int flags = numberOfPeaks;
int placedFlags = CountPlacedFlags(peaks, flags);
while (placedFlags != flags--)
placedFlags = CountPlacedFlags(peaks, flags);
return placedFlags;
}
private static int BinarySearch(int[] peaks)
{
int numberOfPeaks = peaks.Length;
int high = numberOfPeaks;
int low = 0;
int placedFlags = CountPlacedFlags(peaks, high);
if (placedFlags == high)
return placedFlags;
int lastEqualResult = 0;
while (low <= high)
{
int middle = (high - low) / 2 + low;
placedFlags = CountPlacedFlags(peaks, middle);
if (placedFlags == middle)
{
lastEqualResult = placedFlags;
low = middle + 1;
}
else
{
// If we went one higher than our last equal result, then the last equal result is it
if (lastEqualResult > 0 && (middle - lastEqualResult) == 1)
break;
high = middle - 1;
}
}
return lastEqualResult;
}
private static int CountPlacedFlags(int[] peaks, int flags)
{
int numberOfPeaks = peaks.Length;
// First peak always gets a flag
int numberOfPlacedFlags = 1;
int lastPlacedFlag = 0;
for (int i = 1; i < numberOfPeaks; i++)
{
int distance = peaks[i] - peaks[lastPlacedFlag];
if (distance < flags)
continue;
numberOfPlacedFlags++;
lastPlacedFlag = i;
}
return Math.Min(numberOfPlacedFlags, flags);
}
}
using System;
using System.Collections.Generic;
class Solution {
public int solution(int[] A)
{
int[] peaks = FindPeaks(A);
// For now work backwards
//return CountBackwards(peaks);
// Try a binary search
return BinarySearch(peaks);
}
private static int[] FindPeaks(int[] mountains)
{
int numberOfMountains = mountains.Length;
int lastElevation = mountains[0];
bool possiblePeak = false;
var peaks = new List<int>();
// First and last mountains can never be peaks because they can
// never satisfy A[P - 1] < A[P] < A[P + 1] where 0 < P < N - 1
for (int i = 1; i < numberOfMountains - 1; i++)
{
int elevation = mountains[i];
if (elevation > lastElevation)
{
// Last mountain was definitely not a peak but this one could be
lastElevation = elevation;
possiblePeak = true;
continue;
}
if (elevation < lastElevation && possiblePeak)
// Last mountain was a peak
peaks.Add(i - 1);
lastElevation = elevation;
possiblePeak = false;
}
// Second last mountain could still be a peak
if (numberOfMountains > 2 && possiblePeak && mountains[numberOfMountains - 2] > mountains[numberOfMountains - 1])
peaks.Add(numberOfMountains - 2);
return peaks.ToArray();
}
private static int CountBackwards(int[] peaks)
{
int numberOfPeaks = peaks.Length;
int flags = numberOfPeaks;
int placedFlags = CountPlacedFlags(peaks, flags);
while (placedFlags != flags--)
placedFlags = CountPlacedFlags(peaks, flags);
return placedFlags;
}
private static int BinarySearch(int[] peaks)
{
int numberOfPeaks = peaks.Length;
int high = numberOfPeaks;
int low = 0;
int placedFlags = CountPlacedFlags(peaks, high);
if (placedFlags == high)
return placedFlags;
int lastEqualResult = 0;
while (low <= high)
{
int middle = (high - low) / 2 + low;
placedFlags = CountPlacedFlags(peaks, middle);
if (placedFlags == middle)
{
lastEqualResult = placedFlags;
low = middle + 1;
}
else
{
// If we went one higher than our last equal result, then the last equal result is it
if (lastEqualResult > 0 && (middle - lastEqualResult) == 1)
break;
high = middle - 1;
}
}
return lastEqualResult;
}
private static int CountPlacedFlags(int[] peaks, int flags)
{
int numberOfPeaks = peaks.Length;
// First peak always gets a flag
int numberOfPlacedFlags = 1;
int lastPlacedFlag = 0;
for (int i = 1; i < numberOfPeaks; i++)
{
int distance = peaks[i] - peaks[lastPlacedFlag];
if (distance < flags)
continue;
numberOfPlacedFlags++;
lastPlacedFlag = i;
}
return Math.Min(numberOfPlacedFlags, flags);
}
}
The solution obtained perfect score.