You are given positions on a 1D line:
-
people
: an array of
N
integers (positions of people)
-
shops
: an array of
M
integers (positions of milk-tea shops)
You may match a shop to a person if the delivery distance is within a fixed limit D:
∣shops[k]−people[i]∣≤D
Constraints:
-
Each shop can deliver to
at most one
person.
-
Each person can receive from
at most one
shop.
Objective (lexicographic)
-
Maximize
the number of deliveries (matched pairs).
-
Among all matchings that achieve the maximum number of deliveries,
minimize
the
total
delivery distance:
∑∣shops[k]−people[i]∣
Task
Given people, shops, and D (e.g., D = 5), return:
-
maxDeliveries
: the maximum number of deliveries possible
-
minTotalDistance
: the minimum total distance among matchings that achieve
maxDeliveries
Notes
-
Positions may be unsorted; you may sort them.
-
Specify and handle edge cases such as duplicates, empty arrays, and when no pairs are feasible.