MediumArrayDynamic ProgrammingBacktrackingBit ManipulationBitmask

Campus Bikes II

LeetCode
1 approach, code in all languages

On a campus grid you are given the positions of n workers and m bikes, each as [x, y] coordinates, with n <= m. You must assign each worker exactly one distinct bike.

The cost of an assignment is the sum over all workers of the Manhattan distance between a worker and the bike given to that worker. Return the minimum possible total Manhattan distance across all valid assignments.

Example 1

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

Output: 6

Worker 0 takes bike 0 (distance 3) and worker 1 takes bike 1 (distance 3), giving a total of 6, which beats the alternative assignment.

Example 2

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

Output: 4

Assigning bike 0 to worker 0, bike 2 to worker 1, and bike 1 to worker 2 yields the minimum total distance of 4.

Constraints

  • n == workers.length
  • m == bikes.length
  • 1 <= n <= m <= 10
  • workers[i].length == bikes[j].length == 2
  • 0 <= xi, yi < 1000
  • All worker and bike locations are unique.
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now