![]() |
VOOZH | about |
Given two integers n and m, where n represents delivery drivers and m represents the number of packages, Additionally, their position are also given in drivers[][] and packages[][] respectively. The task is to allocate each driver a unique package such that the sum of total Manhattan distances between the drivers and their respective packages are minimized.
Constraints: 1 <= n <= m <= 10
Example:
Input: drivers = {{0,0},{2,1}}, packages = {{1,2},{3,3}}
Output: 6
Explanation: Allocate package 0 to driver 0, and package 1 to driver 1. The Manhattan distance of both allotments is 3, so the output is 6.Input: arr1 = {{0,0},{1,1},{2,0}}, arr2 = {{1,0},{2,2},{2,1}}
Output: 4
Approach:
This approach uses a technique called bitmasking to keep track of which packages have been assigned to drivers. Bitmasking is a way to use bits to represent a set of items. In this case, each bit in an integer represents a package.
Imagine you have a row of light bulbs (packages) that can be either on (assigned) or off (available). This row of light bulbs is represented by an integer, where each bit in the integer is a light bulb. A bit value of 0 means the light bulb is off (the package is available), and a bit value of 1 means the light bulb is on (the package is assigned).
Now, for each driver, starting from the first one, we go through the packages and assign an available package to the driver. We can check if a package is available by looking at the corresponding bit in our integer. If the bit is 0, the package is available.
When we assign a package to a driver, we need to mark it as unavailable for the other drivers. We do this by changing the corresponding bit in our integer from 0 to 1.
In this approach, we use bitwise operations to check, set, and unset bits in our integer. Here’s how:
- Bitwise AND (&): We can use this operation to check if a bit is set (1). If the result of bitmask & (1 << i) is not 0, then the ith bit is set.
- Bitwise OR (|): We can use this operation to set a bit (make it 1). The expression bitmask | (1 << i) will set the ith bit.
- Bitwise XOR (^): We can use this operation to unset a bit (make it 0). The expression bitmask ^ (1 << i) will unset the ith bit if it is set.
Steps-by-step approach:
Below is the implementation of the above approach:
6
Time Complexity: O(2^n * n^2), where n is the maximum number of packages (10 in this case).
Auxiliary space: O(2^n), where n is the maximum number of packages (10 in this case).
To address the task of minimizing the total Manhattan distances in the assignment of drivers to packages more efficiently than the brute-force bitmask approach, we can leverage the Hungarian Algorithm (or Munkres-Kuhn algorithm). This algorithm is specifically designed for solving assignment problems optimally in polynomial time, making it suitable for this scenario where we need to assign n drivers to m packages with the least possible cost.
Steps:
6
Time Complexity: O(n^3), where n is the maximum number of drivers or packages.
Auxiliary space: O(n^2), due to the storage of the cost matrix which is of size n×m.