Campus Bikes II Problem
Description
LeetCode Problem 1066.
On a campus represented as a 2D grid, there are n workers and m bikes, with n <= m. Each worker and bike is a 2D coordinate on this grid.
We assign one unique bike to each worker so that the sum of the Manhattan distances between each worker and their assigned bike is minimized.
Return the minimum possible sum of Manhattan distances between each worker and their assigned bike.
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: 6
Explanation:
We assign bike 0 to worker 0, bike 1 to worker 1. The Manhattan distance of both assignments is 3, so the output is 6.
Example 2:

Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]]
Output: 4
Explanation:
We first assign bike 0 to worker 0, then assign bike 1 to worker 1 or worker 2, bike 2 to worker 2 or worker 1. Both assignments lead to sum of the Manhattan distances as 4.
Example 3:
Input: workers = [[0,0],[1,0],[2,0],[3,0],[4,0]], bikes = [[0,999],[1,999],[2,999],[3,999],[4,999]]
Output: 4995
Constraints:
n == workers.lengthm == bikes.length1 <= n <= m <= 10workers[i].length == 2bikes[i].length == 20 <= workers[i][0], workers[i][1], bikes[i][0], bikes[i][1] < 1000- All the workers and the bikes locations are unique.
Solution
Approach: Bitmask Dynamic Programming
Unlike Campus Bikes I, a greedy choice does not work here: the closest bike for one worker can be the only good bike for another. With at most 10 bikes, though, we can afford to track exactly which bikes are taken, as a bitmask of m bits.
Give the workers their bikes in order, worker 0 first. Then the set of taken bikes also tells us how many workers are done: if mask has c bits set, workers 0..c - 1 have bikes and worker c is next. Let best[mask] be the smallest total distance for that situation, with best[0] = 0.
Go through the masks in increasing order (adding a bike only makes a mask larger, so every mask is final before we use it). From a mask with c < n bits set, try every free bike k for worker c:
best[mask | 1 << k] = min(best[mask | 1 << k], best[mask] + dist(worker c, bike k))
The answer is the smallest best[mask] over masks with exactly n bits set: all workers placed, and any n of the bikes used.
Complexity: O(2m · m) time and O(2m) space; with m <= 10 that is about ten thousand steps.
class Solution:
def assignBikes(self, workers: List[List[int]], bikes: List[List[int]]) -> int:
n, m = len(workers), len(bikes)
# best[mask]: least total distance when the bikes in mask go to the first
# popcount(mask) workers. The next worker to place is always worker popcount(mask).
inf = float("inf")
best = [inf] * (1 << m)
best[0] = 0
ans = inf
for mask in range(1 << m):
if best[mask] == inf:
continue
i = bin(mask).count("1")
if i == n:
ans = min(ans, best[mask])
continue
wx, wy = workers[i]
for k, (bx, by) in enumerate(bikes):
if not mask >> k & 1:
nxt = mask | 1 << k
best[nxt] = min(best[nxt], best[mask] + abs(wx - bx) + abs(wy - by))
return ans