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

example_1.py โ€” Basic Assignment
$ Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]]
โ€บ Output: 6
๐Ÿ’ก Note: Assign bike 0 to worker 0 (distance = |0-1| + |0-2| = 3) and bike 1 to worker 1 (distance = |2-3| + |1-3| = 3). Total distance = 3 + 3 = 6.
example_2.py โ€” Multiple Options
$ Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1],[1,2]]
โ€บ Output: 4
๐Ÿ’ก Note: Optimal assignment: worker 0 gets bike 0 (distance=1), worker 1 gets bike 2 (distance=1), worker 2 gets bike 1 (distance=2). Total = 4.
example_3.py โ€” Edge Case
$ Input: workers = [[0,0]], bikes = [[0,0],[1,1]]
โ€บ Output: 0
๐Ÿ’ก Note: Only one worker needs assignment. Choose the closest bike at position [0,0] with distance 0.

Visualization

Tap to expand
Campus Bike AssignmentW1W2W3B1B2B3B4Optimal Assignment: Total Distance Minimized
Understanding the Visualization
1
Map the Campus
Plot all worker and bike locations on the campus grid
2
Calculate Distances
Compute Manhattan distance between each worker-bike pair
3
Use Smart States
Use bitmasks to efficiently represent which bikes are assigned
4
Memoize Results
Cache results for each state to avoid redundant calculations
5
Find Optimal Assignment
Return the minimum total distance across all possible assignments
Key Takeaway
๐ŸŽฏ Key Insight: Using bitmask DP allows us to efficiently represent assignment states and use memoization to avoid recalculating identical subproblems, achieving optimal performance for this combinatorial optimization problem.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— 2^m)

We have n workers and 2^m possible bike assignment states. Each state is computed once due to memoization.

n
2n
โœ“ Linear Growth
Space Complexity
O(2^m)

We store memoization results for each possible bike assignment bitmask, plus recursion stack space

n
2n
โœ“ Linear 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
Asked in
Google 45 Amazon 35 Microsoft 28 Meta 22
28.4K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen