Trains the technique from
LeetCode 896Monotonic ArrayThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A survey drone logs its altitude relative to sea level once a second, giving the array nums. Altitudes may be negative when the drone flies below sea level.
The flight is called one-way when the drone never reverses its vertical direction: either every reading is greater than or equal to the reading before it, or every reading is less than or equal to the reading before it. Holding altitude is not a reversal, so a run of equal readings is fine either way, and a log with a single reading is one-way.
Return true when the log is one-way and false otherwise.
Example 1
Each reading is at least as high as the one before it, so the drone never descends.
Example 2
The drone climbs from 1 to 2 and later descends from 2 to 1, so the log holds both a rise and a fall.
Example 3
The drone holds 2 and then descends, and holding is not a reversal, so every reading is at most the reading before it.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def is_monotonic(nums: list[int]) -> bool:public boolean isMonotonic(int[] nums)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.