Problem 312104 · medium · Phase 03 Linear Management & Searching

Stops Ordered by Distance from the Depot

sorting · geometry · multi-key sort

Delivery stops are points [x, y] on a grid and the depot is at depot = [dx, dy]. Return the stops ordered from nearest to farthest by Euclidean distance from the depot. When two stops are equally far, order them by smaller x first, then smaller y.

Return a list of the original [x, y] lists.

Examples

Input:  stops = [[3, 3], [1, 1], [2, 0], [0, 2]], depot = [1, 1]
Output: [[1, 1], [0, 2], [2, 0], [3, 3]]
Explanation: [0, 2] and [2, 0] are both at distance sqrt(2); the smaller x wins.

Constraints

  • 0 <= len(stops) <= 10**5
  • -10**4 <= x, y <= 10**4; duplicate stops may appear.
  • Target complexity: O(n log n). Compare exact squared distances, not rounded floats.

Goals

  • Compare distances with squared values to avoid floating point
  • Build a compound key: distance, then x, then y
  • Return the full ordering rather than a prefix
Starting Python…