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: [0,2]
💡 Note: Worker 0 at (0,0): distances to bikes are [3,6,3]. Worker 1 at (2,1): distances are [2,3,0]. Closest pair is (worker 1, bike 2) with distance 0, so assign worker 1 → bike 2. Next closest available is (worker 0, bike 0) with distance 3, so worker 0 → bike 0.
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 - Greedy Assignment INPUT 0 1 2 3 W0 W1 B0 B1 B2 Worker Bike workers = [[0,0],[2,1]] bikes = [[1,2],[3,3],[2,1]] Manhattan Distance d = |x1-x2| + |y1-y2| ALGORITHM STEPS 1 Calculate Distances Compute all worker-bike pairs Dist B0 B1 B2 W0 3 6 3 W1 2 3 0 2 Sort by Priority dist, worker idx, bike idx (0,W1,B2) (2,W1,B0) (3,W0,B0) (3,W0,B2) (3,W1,B1) (6,W0,B1) 3 Greedy Assign Process sorted pairs 4 Skip if Used Track assigned workers/bikes W1 -- B2 (d=0) W0 -- B1 (d=6) FINAL RESULT W0 B1 W1 B2 Output Array [1, 2] answer[0] = 1 (W0 -- B1) answer[1] = 2 (W1 -- B2) OK - Valid Key Insight: The greedy approach sorts all (distance, worker, bike) tuples and assigns bikes in order. Priority: shortest distance first, then smallest worker index, then smallest bike index. W1 gets B2 first (d=0), then W0 gets B1 (d=6) since B0 and B2 unavailable or farther. TutorialsPoint - Campus Bikes | Greedy Approach
Asked in
Google 45 Amazon 38 Facebook 32 Apple 28
78.6K 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