Campus Bikes - Problem

On a campus represented on the X-Y plane, there are n workers and m bikes, with n ≤ m. You are given an array workers of length n where workers[i] = [xi, yi] is the position of the ith worker. You are also given an array bikes of length m where bikes[j] = [xj, yj] is the position of the jth bike.

Assign a bike to each worker using the following rules:

1. Among all available (worker, bike) pairs, choose the one with the shortest Manhattan distance
2. If there are ties, choose the pair with the smallest worker index
3. If there are still ties, choose the pair with the smallest bike index
4. Repeat until all workers have bikes

Return an array answer where answer[i] is the index of the bike assigned to the ith worker.

The Manhattan distance between two points p1 and p2 is: |p1.x - p2.x| + |p1.y - p2.y|

Input & Output

Example 1 — Basic Assignment
$ Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3],[2,1]]
Output: [1,2]
💡 Note: Worker 0 at (0,0): distances to bikes are [3,6,3]. Worker 1 at (2,1): distances are [3,3,1]. Closest pair is (worker 1, bike 2) with distance 1, so assign worker 1 → bike 2. Next closest available is (worker 0, bike 1) with distance 6, so worker 0 → bike 1.
Example 2 — 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 2. When tied, we choose smaller worker index first, then smaller bike index. Worker 0 gets bike 0 (distance 1), worker 1 gets bike 2 (distance 1), worker 2 gets bike 1 (distance 2).
Example 3 — Single Worker
$ Input: workers = [[0,0]], bikes = [[1,1],[2,2],[3,3]]
Output: [0]
💡 Note: Only one worker at (0,0). Distances to bikes are [2,4,6]. Closest bike is at index 0 with distance 2, so assign worker 0 → bike 0.

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 locations are unique

Visualization

Tap to expand
Campus Bikes: Problem OverviewWorkers:0(0,0)1(2,1)Bikes:0(1,2)1(3,3)2(2,1)Assignment Process:1. Calculate all distances using Manhattan formula2. Sort by distance → worker index → bike index3. Assign greedily: (W1,B2)=1, then (W0,B1)=6dist=1 ✓dist=6 ✓Result: [1, 2] - Worker i gets Bike result[i]
Understanding the Visualization
1
Input
Workers and bikes at specific coordinates
2
Calculate Distances
Find Manhattan distance between all pairs
3
Greedy Assignment
Assign based on distance and tie-breaking rules
Key Takeaway
🎯 Key Insight: Greedy assignment with proper tie-breaking (distance → worker → bike) ensures optimal solution
Asked in
Google 45 Amazon 38 Facebook 32 Apple 28
78.5K Views
Medium Frequency
~25 min Avg. Time
1.5K 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