Campus Bikes Problem
Description
LeetCode Problem 1057.
On a campus represented on the X-Y plane, there are n workers and m bikes, with n <= m.
You are given an array workers of length n where workers[i] = [x_i, y_i] is the position of the i^th worker. You are also given an array bikes of length m where bikes[j] = [x_j, y_j] is the position of the j^th bike. All the given positions are unique.
Assign a bike to each worker. Among the available bikes and workers, we choose the (worker_i, bike_j) pair with the shortest Manhattan distance between each other and assign the bike to that worker.
If there are multiple (worker_i, bike_j) pairs with the same shortest Manhattan distance, we choose the pair with the smallest worker index. If there are multiple ways to do that, we choose the pair with the smallest bike index. Repeat this process until there are no available workers.
Return an array answer of length n, where answer[i] is the index (0-indexed) of the bike that the i^th worker is assigned to.
The Manhattan distance between two points p1 and p2 is Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|.
Example 1:

Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]]
Output: [1,0]
Explanation: Worker 1 grabs Bike 0 as they are closest (without ties), and Worker 0 is assigned Bike 1. So the output is [1, 0].
Example 2:

Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]]
Output: [0,2,1]
Explanation: Worker 0 grabs Bike 0 at first. Worker 1 and Worker 2 share the same distance to Bike 2, thus Worker 1 is assigned to Bike 2, and Worker 2 will take Bike 1. So the output is [0,2,1].
Constraints:
n == workers.lengthm == bikes.length1 <= n <= m <= 1000workers[i].length == bikes[j].length == 20 <= x_i, y_i < 10000 <= x_j, y_j < 1000- All worker and bike locations are unique.
Solution
Approach: Bucket the Pairs by Distance
The process in the statement is a greedy matching: always take the closest free (worker, bike) pair, breaking ties by worker index and then by bike index. So if we list all n · m pairs sorted by (distance, worker, bike) and walk through them, every pair whose worker and bike are both still free is exactly the next pair the process would choose.
Sorting a million pairs works, but there is a cheaper way. Coordinates are below 1000, so a Manhattan distance is at most 1998. Put each pair in a bucket for its distance. If we fill the buckets with the worker in the outer loop and the bike in the inner loop, each bucket is already in tie-break order, so the buckets read from distance 0 upwards give the sorted list for free.
Walk the buckets in that order, assign a pair when both of its sides are free, and stop as soon as every worker has a bike.
Complexity: O(n · m) time and space for the pairs, plus a constant 2000 buckets.
class Solution:
def assignBikes(self, workers: List[List[int]], bikes: List[List[int]]) -> List[int]:
n, m = len(workers), len(bikes)
# Distances are at most 999 + 999, so bucket the pairs by distance instead of sorting.
# Filling the buckets worker by worker, bike by bike keeps each bucket in tie-break order.
buckets = [[] for _ in range(2000)]
for i, (wx, wy) in enumerate(workers):
for j, (bx, by) in enumerate(bikes):
buckets[abs(wx - bx) + abs(wy - by)].append(i * m + j)
ans = [-1] * n
bike_used = [False] * m
left = n # workers still without a bike
for bucket in buckets:
for code in bucket:
i, j = divmod(code, m)
if ans[i] == -1 and not bike_used[j]:
ans[i] = j
bike_used[j] = True
left -= 1
if left == 0:
break
return ans