Problem 208038 · easy · Level 02 Linear Data Structures

First Drop

arrays · iteration · first-index

A lap timer records the cumulative distance a robot has driven in readings. The readings should never decrease. Return the index of the first reading that is smaller than the reading before it, or -1 if the readings never drop.

Examples

Input:  readings = [1, 2, 3, 2, 5]
Output: 3
Explanation: readings[3] = 2 is smaller than readings[2] = 3.

Input:  readings = [1, 1, 2]
Output: -1
Explanation: Staying equal is not a drop.

Constraints

  • 0 <= len(readings) <= 10**5
  • Target: O(n) time; stop as early as possible.

Goals

  • Return as soon as a condition is met
  • Return a sentinel when the condition never occurs
Starting Python…