Tap here to switch tabs
Problem
Submissions

Problem: Campus Bikes

med
30 min
Try to solve the Campus Bikes problem.

Statement

On a campus represented by a 22D grid, there are n workers and m bikes, where n≤mn \leq m.

You are given an array workers of length n where workers[i] = [x_i, y_i] represents the position of the iextthi^{ ext{th}} worker, and an array bikes of length m where bikes[j] = [x_j, y_j] represents the position of the jextthj^{ ext{th}} bike. All coordinates are given on a 22D plane.

Each worker must be assigned exactly one bike, and each bike can be assigned to at most one worker. The assignments are made greedily according to the following priority rules:

  1. Pairs of (worker, bike) with a smaller Manhattan distance are assigned first. The Manhattan distance between worker i and bike j is ∣xi−xj∣+∣yi−yj∣|x_i - x_j| + |y_i - y_j|.

  2. If multiple pairs share the same distance, priority is given to the pair with the smaller worker index.

  3. If multiple pairs share the same distance and worker index, priority is given to the pair with the smaller bike index.

Once a worker is assigned a bike or a bike is taken by a worker, neither can be part of any future assignment.

Return an array result of length n, where result[i] is the 00 indexed index of the bike assigned to the iextthi^{ ext{th}} worker.

Constraints:

  • n == workers.length

  • m == bikes.length

  • 1≤1 \leq n ≤\leq m ≤1000\leq 1000

  • workers[i].length == bikes[j].length == 22

  • 0≤xi,yi<10000 \leq x_i, y_i < 1000

  • 0≤xj,yj<10000 \leq x_j, y_j < 1000

  • All worker and bike locations are unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Campus Bikes

med
30 min
Try to solve the Campus Bikes problem.

Statement

On a campus represented by a 22D grid, there are n workers and m bikes, where n≤mn \leq m.

You are given an array workers of length n where workers[i] = [x_i, y_i] represents the position of the iextthi^{ ext{th}} worker, and an array bikes of length m where bikes[j] = [x_j, y_j] represents the position of the jextthj^{ ext{th}} bike. All coordinates are given on a 22D plane.

Each worker must be assigned exactly one bike, and each bike can be assigned to at most one worker. The assignments are made greedily according to the following priority rules:

  1. Pairs of (worker, bike) with a smaller Manhattan distance are assigned first. The Manhattan distance between worker i and bike j is ∣xi−xj∣+∣yi−yj∣|x_i - x_j| + |y_i - y_j|.

  2. If multiple pairs share the same distance, priority is given to the pair with the smaller worker index.

  3. If multiple pairs share the same distance and worker index, priority is given to the pair with the smaller bike index.

Once a worker is assigned a bike or a bike is taken by a worker, neither can be part of any future assignment.

Return an array result of length n, where result[i] is the 00 indexed index of the bike assigned to the iextthi^{ ext{th}} worker.

Constraints:

  • n == workers.length

  • m == bikes.length

  • 1≤1 \leq n ≤\leq m ≤1000\leq 1000

  • workers[i].length == bikes[j].length == 22

  • 0≤xi,yi<10000 \leq x_i, y_i < 1000

  • 0≤xj,yj<10000 \leq x_j, y_j < 1000

  • All worker and bike locations are unique.