Problem 204145 · medium · Level 02 Linear Data Structures

Fix One Reading

arrays · checks · neighbours

An odometer should produce non-decreasing readings, but at most one reading may be corrupted. Return True if readings can be made non-decreasing by changing at most one element (to any value), and False otherwise.

Examples

Input:  readings = [4, 2, 3]
Output: True
Explanation: Change the 4 to 1 (or the 2 to 4).

Input:  readings = [4, 2, 1]
Output: False

Input:  readings = [3, 4, 2, 3]
Output: False

Constraints

  • 0 <= len(readings) <= 10**5
  • -10**9 <= readings[i] <= 10**9
  • Target: O(n) time; do not try every possible change.

Goals

  • Allow exactly one violation while validating an ordering
  • Decide which of two neighbours to adjust by looking one element further back
Starting Python…