PracticeArrays

Find the Lightest Shipment

BeginnerArraystraversalstate trackinginvariants Time not estimated Not started

Context

A regional warehouse records the weights of packages loaded onto one delivery route. The dispatch team needs to know the lightest package in the shipment list so it can choose suitable handling equipment. Inspect the recorded weights and report the smallest one.

Problem

Implement a function that receives the package weights for one delivery route and returns the minimum weight. Start with an available package as the current minimum, then scan the remaining weights once. Whenever a smaller weight is found, replace the current minimum. Package weights may be negative, zero, repeated, or all identical. The input always contains at least one weight, so a minimum exists.

Examples

Example 1
Input: weights = [42]
Output: 42
Explanation: For [42], the executed result is 42, and the final current_best is 42 with cursor equal to 1.
Example 2
Input: weights = [7, 7, 7, 7]
Output: 7
Explanation: For [7, 7, 7, 7], the executed result is 7, and the final current_best is 7 with cursor equal to 4.
Example 3
Input: weights = [12, 0, -5, 8, -5, 3]
Output: -5
Explanation: For [12, 0, -5, 8, -5, 3], the executed result is -5, and the final current_best is -5 with cursor equal to 6.

Constraints

  • 1 <= len(weights) <= 100000
  • -1000000 <= weights[i] <= 1000000 for every i
  • weights: non empty
  • Types: weights is int[]; result is int

Function signature

def find_lightest_shipment(weights: list[int]) -> int
weights list[int]
The recorded integer weights of packages on one delivery route.
returns int
The smallest package weight in weights.

Adapted from MBPP problem task_62 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Approved · c001r1-abs_516_v314 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1