28. Valid Mountain Walk

EasyArray

A hiking guide describes a trail by listing the altitude at each checkpoint. A trail is a proper mountain walk if it climbs strictly upward for at least one step, reaches a single peak, and then descends strictly downward for at least one step, never staying level and never going up again after the peak.

Write a method isMountain that takes the array nums of checkpoint altitudes and returns true if the trail is a proper mountain walk, and false otherwise.

Example 1

Input:
nums = [1,3,5,4,2]
Output:
true
Explanation:

The altitude rises 1, 3, 5 and then falls 4, 2, so this is a mountain.

Example 2

Input:
nums = [1,2,3]
Output:
false
Explanation:

The trail only climbs; it never comes back down, so it is not a mountain.

Constraints

1 ≤ nums.length ≤ 1000

0 ≤ nums[i] ≤ 104

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…