On a campus grid you are given the positions of n workers and m bikes, each as [x, y] coordinates, with n <= m. You must assign each worker exactly one distinct bike.
The cost of an assignment is the sum over all workers of the Manhattan distance between a worker and the bike given to that worker. Return the minimum possible total Manhattan distance across all valid assignments.
Example 1
Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]]
Output: 6
Worker 0 takes bike 0 (distance 3) and worker 1 takes bike 1 (distance 3), giving a total of 6, which beats the alternative assignment.
Example 2
Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]]
Output: 4
Assigning bike 0 to worker 0, bike 2 to worker 1, and bike 1 to worker 2 yields the minimum total distance of 4.
Constraints
n == workers.lengthm == bikes.length1 <= n <= m <= 10workers[i].length == bikes[j].length == 20 <= xi, yi < 1000All worker and bike locations are unique.See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI