MountainRanges
SRM 634 · 2014-08-25 · by lg5293
Problem Statement
Tom is in charge of a tourist agency. He has a lovely picture of the local mountain range. He would like to sell it to the tourists but first he needs to know how many peaks are visible in the picture.
The mountain range in the picture can be seen as a sequence of heights.
You are given these heights as a
Constraints
- heights will contain between 1 and 50 elements, inclusive.
- Each element of heights will be between 1 and 100, inclusive.
{5, 6, 2, 4}
Returns: 2
Element 1 (0-based index) is a peak. Its height is 6, which is strictly greater than each of its neighbors' heights (5 and 2). Element 3 is also a peak since its height is 4 and that is strictly greater than its neighbor's height (which is 2).
{1, 1, 1, 1, 1, 1, 1}
Returns: 0
This is a very flat mountain with no peaks.
{2, 1}
Returns: 1
Element 0 is a peak.
{2,5,3,7,2,8,1,3,1}
Returns: 4
The peaks here are the elements with 0-based indices 1, 3, 5, and 7. Their heights are 5, 7, 8, and 3, respectively.
{1}
Returns: 1
Element 0 is a peak. Even though it has no neighbors, the condition from the problem statement is still satisfied.
{1,2,3,4,4,3,2,1}
Returns: 0
According to our definition there is no peak in this mountain range.
Submissions are judged against all 92 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class MountainRanges with a public method int countPeaks(vector<int> heights) · 92 test cases · 2 s / 256 MB per case