![]() |
VOOZH | about |
On a 2D grid representing a campus, there are n workers and m bikes, with 1 <= n <= m <= 10. Each worker and bike is assigned a 2D coordinate on this grid. The tasks is to assign one unique bike to each worker in a way that minimizes the sum of the Manhattan distances between each worker and their assigned bike. The task is to return the minimum possible sum of Manhattan distances between each worker and their assigned bike.
The Manhattan distance between two points p1 and p2 is calculated as follows: Manhattan(p1, p2) = |p1.x - p2.x| + |p1.y - p2.y|
Example:
Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]]
Output: 6
Explanation: We assign bike 0 to worker 0, bike 1 to worker 1. The Manhattan distance of both assignments is 3, so the output is 6.Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]]
Output: 4
Explanation: We first assign bike 0 to worker 0, then assign bike 1 to worker 1 or worker 2, bike 2 to worker 2 or worker 1. Both assignments lead to sum of the Manhattan distances as 4.
Approach: (Top-Down Dynamic Programming + BitMasking)
There are two elements of this problem that serve as hints for another way to approach the problem:
- The problem requires us to minimize the distance sum by making sequential decisions (assigning bikes to workers).
- Each decision we make is affected by the previous decisions we made (which bikes are available depends on which bikes have already been assigned). These are both characteristics of problems that can be solved using dynamic programming. Thus, in this approach, we will use recursive dynamic programming.
In this approach, we will be using bits to represent mark the availability of bikes. Since the maximum number of bikes is less than 32, we can use bitmasking to represent which bikes have been taken with a single integer.
The availability of bikes is now represented by an integer mask having 10 bits. The 10 bits represent the states of 10 bikes. A value of 0 at the ith bit signifies that the bike at the ith index is available while a value of 1 signifies that the bike has been assigned to a worker.
For every worker starting from the worker at index 0, we will traverse over the bikes and assign it to the worker if it is available. Availability of ith bike can be checked by the ith bit in mask, the bike is available if the ith bit in mask is 0. When we assign a bike to the worker we should mark it as unavailable for further workers and for that we need to change the ith bit to 1.
In this approach we need to check/set/unset a particular bit in an integer.
The below slides show how bitwise AND (&) can be used to check if the ith bit is set, how bitwise OR (|) can be used to set the ith bit, and how bitwise XOR (^) can be used to unset the ith bit.
Steps-by-step approach::
Below is the implementation of the above approach:
Minimum distance sum for assigning bikes: 6
Time complexity: O(M* 2^M), Here N is the number of workers, and M is the number of bikes.
Auxiliary Space: O(2^ M)
Related Article: