Campus Bikes II
1 approach, code in every language
Problem description
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.
Create a free account to see the solution
You get the step-by-step animation, the intuition and clean code in every language. Free, no credit card.
After the pattern
Now solve one you haven't seen.
Reading a pattern isn't the same as using it. The practice set has 1,194 original problems on the same techniques, each graded in your browser.
1,194 problems · 15,062 graded tests · free