Ctrl K

One Climb, One Descent

Easy
A hiker wrote down the altitudes along a trail, in order, in the array heights. The trail counts as a "single peak" when: - it has at least three points; - the altitudes first strictly increase and then strictly decrease; - both the climb and the descent are non-empty, so the summit is neither the first nor the last point. Flat stretches (two neighbouring points at the same altitude) are not allowed. Return True if the trail is a single peak, and False otherwise.
Example 1
heights=[1, 4, 7, 3, 0] → True

The heights rise strictly up to 7 and then fall strictly.

Example 2
heights=[2, 6, 6, 1] → False

Two neighbouring 6s make a flat stretch, which is not allowed.

Example 3
heights=[3, 5, 8] → False

There is a climb but no descent.

Constraints
  • 1 ≤ len(heights) ≤ 10⁴
  • 0 ≤ heights[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
heights=[1, 4, 7, 3, 0]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 120 tests on the server, hidden ones included.