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.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.
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.
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.
heights=[2, 5, 9, 7, 8]
2
Run checks the first 3 tests in your browser. Submit checks all 135 tests on the server, hidden ones included.