Ctrl K

Points for the Climb

Medium
The array heights lists the heights of points along a route. Every inner point (neither the first nor the last) earns points: - **2 points** if it is strictly higher than *every* point before it and strictly lower than *every* point after it; - **1 point** if the first rule fails, but the point is strictly higher than its left neighbour and strictly lower than its right neighbour; - **0 points** otherwise. Return the total score of all inner points. Checking "everything before and after" naively is O(n²); aim for linear time.
Example 1
heights=[2, 5, 9, 7, 8] → 2

5 is above everything to its left (2) and below everything to its right (9, 7, 8): 2 points. 9 beats its left neighbour but not its right one: 0. 7 is below its left neighbour: 0. Total 2.

Example 2
heights=[1, 4, 3, 5, 6] → 2

4 is above 1, but its right neighbour 3 is smaller, so 0. 3 is below its left neighbour: 0. 5: everything to the left (1, 4, 3) is smaller and everything to the right (6) is larger: 2. Total 2.

Example 3
heights=[3, 1, 2, 5, 4] → 1

1 and 5 fail even the neighbour check. 2 sits between neighbours 1 and 5, but there is a 3 further left, so it earns 1 point. Total 1.

Constraints
  • 3 ≤ len(heights) ≤ 10⁵
  • 1 ≤ heights[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Boards Holding Water MediumZero-Sum Triples MediumTriple Sum Nearest the Target MediumFour Numbers to a Target MediumNext Arrangement in Order Medium
loading Python…
Input
heights=[2, 5, 9, 7, 8]
Expected
2

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