43. Steady Trend

A weather station logs a reading once an hour and stores the readings in values. The log is called steady if it never changes direction: either no reading is lower than the one before it (it only rises or stays level), or no reading is higher than the one before it (it only falls or stays level).

Return true if the log is steady and false otherwise. A log with one reading, or with all readings equal, is steady.

Solve it in a single pass over the array, without sorting or copying it.

Example 1

Input:
values = [1,2,2,5]
Output:
true
Explanation:

No reading ever drops, so the log is steady.

Example 2

Input:
values = [6,4,5,1]
Output:
false
Explanation:

The log falls, rises and falls again, so it is not steady.

Example 3

Input:
values = [3,3,3]
Output:
true
Explanation:

All readings are equal, so the log is steady.

Constraints

1 ≤ values.length ≤ 105

-109 ≤ values[i] ≤ 109

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…