leetle

Daily challenge for 2026-10-02

640. Sort Segments by Span Length

Given a list of 1D line segments segments where each segment is a pair [start, end] with start <= end, sort the segments in ascending order of their span length (end - start).

If two segments have the same span length, maintain their relative original order (stable sort).

Return the sorted list of segments.

Example:


Input: segments = [[2, 7], [1, 3], [5, 6]]
Output: [[5, 6], [1, 3], [2, 7]]
Explanation: The span lengths are 5 (7 - 2), 2 (3 - 1), and 1 (6 - 5). Sorting by length gives [5, 6] (length 1), then [1, 3] (length 2), then [2, 7] (length 5).

Run this challenge interactively in Python or JavaScript when the app loads.