Check a Shipment Loading Trend
Context
A warehouse records the weights of packages loaded onto one delivery vehicle. The loading record is considered orderly if package weights never decrease from one package to the next, or if they never increase. Equal neighboring weights are allowed.
Problem
Write a function that decides whether the package-weight record is monotonic in at least one direction. Return true if every adjacent pair is nondecreasing or every adjacent pair is nonincreasing; otherwise return false. An empty record and a record with one package are valid because they have no contradictory adjacent pair. Equal weights must preserve both possible directions. Scan the record once and use constant extra space.
Examples
weights = [2, 4, 4, 7]Trueweights = [10, 8, 5, 5, 1]Trueweights = [3, 7, 4, 6]FalseConstraints
0 <= len(weights) <= 1000000 <= weights[i] <= 1000000 for every i- Types:
weightsis int[]; result is bool
Function signature
def is_orderly_loading(weights: list[int]) -> bool
weightslist[int]- Package weights in the order they were loaded.
- returns bool
- True when the weights are nondecreasing or nonincreasing, including records with equal neighboring values; otherwise false.
Adapted from MBPP problem task_68 (CC BY 4.0). Rewritten, extended and verified by Iksha.