09. Assign Mice Holes
β GFG solution to the Assign Mice Holes problem: find optimal assignment to minimize maximum time using greedy approach with sorting. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given two arrays mices[]
and holes[]
of the same size. The array mices[]
represents the positions of the mice on a straight line, while the array holes[]
represents the positions of the holes on the same line. Each hole can accommodate exactly one mouse.
A mouse can either stay in its current position, move one step to the right, or move one step to the left, and each move takes one minute. The task is to assign each mouse to a distinct hole in such a way that the time taken by the last mouse to reach its hole is minimized.
π Examples
Example 1
Input: mices[] = [4, -4, 2], holes[] = [4, 0, 5]
Output: 4
Explanation: Assign the mouse at position 4 to the hole at position 4, so the time taken is 0 minutes.
Assign the mouse at position β4 to the hole at position 0, so the time taken is 4 minutes.
Assign the mouse at position 2 to the hole at position 5, so the time taken is 3 minutes.
Hence, the maximum time required by any mouse is 4 minutes.
Example 2
Input: mices[] = [1, 2], holes[] = [20, 10]
Output: 18
Explanation: Assign the mouse at position 1 to the hole at position 10, so the time taken is 9 minutes.
Assign the mouse at position 2 to the hole at position 20, so the time taken is 18 minutes.
Hence, the maximum time required by any mouse is 18 minutes.
π Constraints
$1 \le \text{mices.size()} = \text{holes.size()} \le 10^5$
$-10^5 \le \text{mices}[i], \text{holes}[i] \le 10^5$
β
My Approach
The optimal approach uses a Greedy Algorithm with Sorting to achieve the minimum maximum time:
Greedy Sorting Strategy
Sort Both Arrays:
Sort the
mices[]
array in ascending order.Sort the
holes[]
array in ascending order.
Greedy Assignment:
Assign the i-th smallest mouse position to the i-th smallest hole position.
This ensures that mice don't "cross over" each other, which would increase travel time.
Calculate Maximum Time:
For each mouse-hole pair, calculate the absolute difference (travel time).
Track the maximum time among all assignments.
Mathematical Proof:
If we have sorted positions and try to swap any two assignments, it will either keep the same maximum time or increase it.
This greedy approach guarantees the optimal solution.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n), where n is the size of the arrays. The sorting operations dominate the complexity, while the linear traversal to find maximum difference takes O(n) time.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables. The sorting is done in-place.
π§βπ» Code (C++)
class Solution {
public:
int assignHole(vector<int>& m, vector<int>& h) {
sort(m.begin(), m.end());
sort(h.begin(), h.end());
int r = 0;
for (int i = 0; i < m.size(); ++i)
r = max(r, abs(m[i] - h[i]));
return r;
}
};
β Code (Java)
class Solution {
public int assignHole(int[] m, int[] h) {
Arrays.sort(m);
Arrays.sort(h);
int r = 0;
for (int i = 0; i < m.length; i++)
r = Math.max(r, Math.abs(m[i] - h[i]));
return r;
}
}
π Code (Python)
class Solution:
def assignHole(self, m, h):
m.sort()
h.sort()
return max(abs(a - b) for a, b in zip(m, h))
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated