There are N Mice and N holes are placed in a straight line. Each hole can accommodate only 1 mouse. A mouse can stay at his position, move one step right from x to x + 1, or move one step left from x to x -1. Any of these moves consumes 1 minute. Assign mice to holes so that the time when the last mouse gets inside a hole is minimized.
Input : positions of mice are: 4 -4 2 positions of holes are: 4 0 5 Output : 4 Assign mouse at position x = 4 to hole at position x = 4 : Time taken is 0 minutes Assign mouse at position x=-4 to hole at position x = 0 : Time taken is 4 minutes Assign mouse at position x=2 to hole at position x = 5 : Time taken is 3 minutes After 4 minutes all of the mice are in the holes. Since, there is no combination possible where the last mouse's time is less than 4, answer = 4. Input : positions of mice are: -10, -79, -79, 67, 93, -85, -28, -94 positions of holes are: -2, 9, 69, 25, -31, 23, 50, 78 Output : 102
This problem can be solved using greedy strategy. We can put every mouse to its nearest hole to minimize the time. This can be done by sorting the positions of mice and holes. This allows us to put the ith mice to the corresponding hole in the holes list. We can then find the maximum difference between the mice and corresponding hole position.
In example 2, on sorting both the lists, we find that the mouse at position -79 is the last to travel to hole 23 taking time 102.
sort mice positions (in any order) sort hole positions Loop i = 1 to N: update ans according to the value of |mice(i) - hole(i)|. It should be maximum of all differences.
Proof of correctness:
Let i1 < i2 be the positions of two mice and let j1 < j2 be the positions of two holes.
It suffices to show via case analysis that
max(|i1-j1|, |i2-j2|) <= max(|i1-j2|, |i2-j1|), where '|a - b|' represent absolute value of (a - b)
Since it follows by induction that every assignment can be transformed by a series of swaps into the sorted assignment, where none of these swaps increases the span.
The last mouse gets into the hole in time: 4
This article is contributed by Saloni Baweja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to find the number from given holes
- Find minimum value to assign all array elements so that array product becomes greater
- Maximum sum of elements in a diagonal parallel to the main diagonal of a given Matrix
- Minimize cost of choosing and skipping array elements to reach end of the given array
- Count positions in a chessboard that can be visited by the Queen which are not visited by the King
- Print first K distinct Moran numbers from a given array
- Count three-digit numbers having difference X with its reverse
- Maximum XOR of a path from top-left to bottom-right cell of given Matrix
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Print X array elements closest to the Kth smallest element in the array
- XOR of array elements whose modular inverse with a given number exists
- Count sequences of given length having non-negative prefix sums that can be generated by given values
- Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
- Check if given Sudoku solution is valid or not
Improved By : aayushgarg06