Imagine you're managing a bike-sharing program on a university campus! ๐ฒ
You have a 2D grid representing the campus with n workers who need bikes and m available bikes scattered across different locations, where n โค m. Each worker and bike has specific coordinates (x, y) on this grid.
Your mission: assign exactly one unique bike to each worker such that the total walking distance is minimized. The distance is measured using Manhattan distance: |xโ - xโ| + |yโ - yโ| (think city blocks!).
Return the minimum possible sum of Manhattan distances between all workers and their assigned bikes.
Key Challenge: Unlike the simpler Campus Bikes I problem, here we want the globally optimal assignment, not just a greedy one!
Input & Output
Visualization
Time & Space Complexity
We have n workers and 2^m possible bike assignment states. Each state is computed once due to memoization.
We store memoization results for each possible bike assignment bitmask, plus recursion stack space
Constraints
- 1 โค workers.length โค 10
- workers.length โค bikes.length โค 10
- workers[i].length == bikes[j].length == 2
- 0 โค workers[i][0], workers[i][1], bikes[j][0], bikes[j][1] < 1000
- Note: Small constraints allow for exponential solutions