Campus Bikes - Problem
Imagine you're managing a bike-sharing program on a university campus! ๐Ÿšดโ€โ™‚๏ธ You have n workers who need bikes and m bikes scattered across campus (where n โ‰ค m). Your job is to efficiently assign one bike to each worker.

The assignment follows a greedy strategy: always pick the worker-bike pair with the shortest Manhattan distance first. If there's a tie in distance, prioritize by smallest worker index, then by smallest bike index.

Manhattan Distance Formula: |x1 - x2| + |y1 - y2|

Goal: Return an array where result[i] is the index of the bike assigned to the i-th worker.

Input & Output

example_1.py โ€” Basic Assignment
$ Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]]
โ€บ Output: [1, 0]
๐Ÿ’ก Note: Worker 0 is at (0,0) and Worker 1 is at (2,1). Distances: W0โ†’B0=3, W0โ†’B1=6, W1โ†’B0=2, W1โ†’B1=2. Smallest distance is 2 (tie between W1โ†’B0 and W1โ†’B1). Choose W1โ†’B0 (smaller bike index). Then W0โ†’B1. Result: [1,0].
example_2.py โ€” Tie-breaking by Worker Index
$ Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]]
โ€บ Output: [0, 2, 1]
๐Ÿ’ก Note: Multiple pairs have distance 1: W0โ†’B0=1, W1โ†’B2=1. W0 has smaller index, so W0โ†’B0 first. Then W2โ†’B1=1 is smallest remaining. Finally W1โ†’B2. Result: [0,2,1].
example_3.py โ€” Single Worker-Bike Pair
$ Input: workers = [[0,0]], bikes = [[1,1]]
โ€บ Output: [0]
๐Ÿ’ก Note: Only one worker and one bike available. Worker 0 gets assigned to Bike 0 (the only option). Manhattan distance is |0-1| + |0-1| = 2.

Visualization

Tap to expand
Campus Bike Assignment VisualizationCampus Grid:W0(0,0)W1(2,1)B0(1,2)B1(3,3)d=3d=6d=2d=2Priority Queue (Sorted Pairs):1. (W1โ†’B0) dist=2 โœ“ ASSIGN2. (W1โ†’B1) dist=2 (tie: W1 < W1, B1 > B0)3. (W0โ†’B0) dist=3 (B0 taken)4. (W0โ†’B1) dist=6 โœ“ ASSIGNAssignment Process:Step 1: Pop minimum (W1โ†’B0, dist=2)โœ“ Both W1 and B0 available โ†’ Assign W1 to B0Step 2: Continue popping until W0 gets a bikeโœ“ Eventually assign W0 to B1Final Result:result = [1, 0]W0โ†’B1, W1โ†’B0Key Insights:โ€ข Manhattan distance = |xโ‚-xโ‚‚| + |yโ‚-yโ‚‚|โ€ข Tie-breaking: distance โ†’ worker index โ†’ bike indexโ€ข Greedy approach guarantees globally optimal solutionโ€ข Priority queue handles sorting automatically
Understanding the Visualization
1
Calculate Distances
Compute Manhattan distance between every worker-bike pair
2
Sort by Priority
Order pairs by distance (asc), worker index (asc), bike index (asc)
3
Greedy Assignment
Pick the best available pair and assign, marking both as used
4
Repeat Until Complete
Continue until all workers have bikes assigned
Key Takeaway
๐ŸŽฏ Key Insight: The greedy strategy works because we always make the locally optimal choice (shortest distance with proper tie-breaking), which leads to the globally optimal assignment for this problem.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nm log(nm))

Generate nm pairs O(nm), sort them O(nm log(nm)), assign O(nm) - dominated by sorting

n
2n
โšก Linearithmic
Space Complexity
O(nm)

Store all worker-bike pairs for sorting

n
2n
โšก Linearithmic Space

Constraints

  • n == workers.length
  • m == bikes.length
  • 1 โ‰ค n โ‰ค m โ‰ค 1000
  • workers[i].length == bikes[j].length == 2
  • 0 โ‰ค workers[i][0], workers[i][1], bikes[j][0], bikes[j][1] < 1000
  • All worker and bike positions are unique.
Asked in
Google 45 Amazon 38 Meta 25 Microsoft 18
67.0K Views
High Frequency
~25 min Avg. Time
1.9K 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