Assign bikes to workers greedily by Manhattan distance with index tie-breaks

Quick Overview

Assign each worker a bike by repeatedly pairing the closest free worker and bike by Manhattan distance, breaking ties by worker index and then by bike index. Tests greedy simulation, ordering with multi-key tie-breaks and bucket sort over a bounded range of distances.

Assign bikes to workers greedily by Manhattan distance with index tie-breaks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

On a campus represented as a 2D grid, there are `n` workers and `m` bikes, with `n <= m`. You are given a list `workers` of length `n` where `workers[i] = [x_i, y_i]` is the position of the `i`-th worker, and a list `bikes` of length `m` where `bikes[j] = [x_j, y_j]` is the position of the `j`-th bike. Assign a bike to each worker. Among the available bikes and workers, choose the (worker, bike) pair with the **shortest Manhattan distance** between them and assign that bike to that worker. If several pairs share the shortest distance, choose the pair with the smallest worker index. If several of those remain, choose the pair with the smallest bike index. Repeat until every worker has a bike. Return a list `answer` of length `n`, where `answer[i]` is the index of the bike assigned to the `i`-th worker. The Manhattan distance between two points `p1` and `p2` is `|p1.x - p2.x| + |p1.y - p2.y|`. ### Function Signature ```python def assign_bikes(workers: list[list[int]], bikes: list[list[int]]) -> list[int]: ``` ### Rules - Worker and bike indices are their 0-based positions in `workers` and `bikes`. - Each step considers only workers without a bike and bikes not yet taken. It picks the pair with the smallest key `(distance, worker index, bike index)`, compared in that order, and then removes both from further consideration. - Once a bike is assigned it stays with that worker. ### Constraints - `n == len(workers)` - `m == len(bikes)` - `1 <= n <= m <= 1000` - `len(workers[i]) == 2` and `len(bikes[j]) == 2` - `0 <= x_i, y_i < 1000` - `0 <= x_j, y_j < 1000` - All worker and bike locations are unique. ### Examples ```text Input: workers = [[0, 0], [2, 1]], bikes = [[1, 2], [3, 3]] Output: [1, 0] ``` The distances are 3 from worker 0 to bike 0, 6 from worker 0 to bike 1, 2 from worker 1 to bike 0 and 3 from worker 1 to bike 1. The closest pair is worker 1 and bike 0 at distance 2, so worker 1 gets bike 0 even though it is also the nearest bike to worker 0, and worker 0 then takes bike 1. ```text Input: workers = [[0, 0], [1, 1], [2, 0]], bikes = [[1, 0], [2, 2], [2, 1]] Output: [0, 2, 1] ``` Five (worker, bike) pairs are at distance 1: (0, 0), (1, 0), (1, 2), (2, 0) and (2, 2). Worker 0 takes bike 0, worker 1 then takes bike 2, and worker 2, whose distance-1 bikes are both gone, takes bike 1 at distance 2. ```text Input: workers = [[0, 0], [2, 0]], bikes = [[1, 0], [5, 0]] Output: [0, 1] ``` Both workers are at distance 1 from bike 0, so the tie goes to the smaller worker index and worker 0 takes bike 0. Worker 1 then takes bike 1 at distance 3.

Overview: Assign each worker a bike by repeatedly pairing the closest free worker and bike by Manhattan distance, breaking ties by worker index and then by bike index. Tests greedy simulation, ordering with multi-key tie-breaks and bucket sort over a bounded range of distances.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

On a campus represented as a 2D grid, there are n workers and m bikes, with n <= m.

You are given a list workers of length n where workers[i] = [x_i, y_i] is the position of the i-th worker, and a list bikes of length m where bikes[j] = [x_j, y_j] is the position of the j-th bike.

Assign a bike to each worker. Among the available bikes and workers, choose the (worker, bike) pair with the shortest Manhattan distance between them and assign that bike to that worker. If several pairs share the shortest distance, choose the pair with the smallest worker index. If several of those remain, choose the pair with the smallest bike index. Repeat until every worker has a bike.

Return a list answer of length n, where answer[i] is the index of the bike assigned to the i-th worker.

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

Function Signature

def assign_bikes(workers: list[list[int]], bikes: list[list[int]]) -> list[int]:

Rules

  • Worker and bike indices are their 0-based positions in workers and bikes .
  • Each step considers only workers without a bike and bikes not yet taken. It picks the pair with the smallest key (distance, worker index, bike index) , compared in that order, and then removes both from further consideration.
  • Once a bike is assigned it stays with that worker.

Constraints

  • n == len(workers)
  • m == len(bikes)
  • 1 <= n <= m <= 1000
  • len(workers[i]) == 2 and len(bikes[j]) == 2
  • 0 <= x_i, y_i < 1000
  • 0 <= x_j, y_j < 1000
  • All worker and bike locations are unique.

Examples

Input: workers = [[0, 0], [2, 1]], bikes = [[1, 2], [3, 3]]
Output: [1, 0]

The distances are 3 from worker 0 to bike 0, 6 from worker 0 to bike 1, 2 from worker 1 to bike 0 and 3 from worker 1 to bike 1. The closest pair is worker 1 and bike 0 at distance 2, so worker 1 gets bike 0 even though it is also the nearest bike to worker 0, and worker 0 then takes bike 1.

Input: workers = [[0, 0], [1, 1], [2, 0]], bikes = [[1, 0], [2, 2], [2, 1]]
Output: [0, 2, 1]

Five (worker, bike) pairs are at distance 1: (0, 0), (1, 0), (1, 2), (2, 0) and (2, 2). Worker 0 takes bike 0, worker 1 then takes bike 2, and worker 2, whose distance-1 bikes are both gone, takes bike 1 at distance 2.

Input: workers = [[0, 0], [2, 0]], bikes = [[1, 0], [5, 0]]
Output: [0, 1]

Both workers are at distance 1 from bike 0, so the tie goes to the smaller worker index and worker 0 takes bike 0. Worker 1 then takes bike 1 at distance 3.