Check Shipment Weight Order
Context
A warehouse loads shipments onto a delivery truck in a planned sequence. The loading supervisor wants to know whether each shipment is no lighter than the one immediately before it, so the weights never decrease along the route. Equal weights are acceptable, and an empty or one-shipment load is considered ordered.
Problem
Implement a function that checks whether the shipment weights are in nondecreasing order from the first loaded shipment to the last. For every neighboring pair, the later weight must be greater than or equal to the earlier weight. If any neighboring pair decreases, the sequence is not ordered. Return true when all adjacent pairs satisfy the rule. Empty and single-element arrays have no violating pair and must return true. Use a single scan with constant extra space; do not sort or rearrange the input.
Examples
weights = [12, 18, 18, 25]Trueweights = [5, 9, 14, 20]Trueweights = [30, 27, 27, 35]FalseConstraints
0 <= len(weights) <= 100000-1000000 <= weights[i] <= 1000000 for every i- Types:
weightsis int[]; result is bool
Function signature
def is_shipment_ordered(weights: list[int]) -> bool
weightslist[int]- Shipment weights listed in the order they are loaded onto the delivery truck.
- returns bool
- True if the shipment weights never decrease from one adjacent shipment to the next; otherwise false.
Adapted from MBPP problem task_567 (CC BY 4.0). Rewritten, extended and verified by Iksha.