-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmice.java
More file actions
47 lines (35 loc) · 1.34 KB
/
mice.java
File metadata and controls
47 lines (35 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
There are N Mice and N holes are placed in a straight line.
Each hole can accomodate 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.
Example:
positions of mice are:
4 -4 2
positions of holes are:
4 0 5
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:
A : list of positions of mice
B : list of positions of holes
Output:
single integer value
NOTE: The final answer will fit in a 32 bit signed integer.
*/
public int mice(ArrayList<Integer> a, ArrayList<Integer> b) {
if(a.isEmpty() || a==null || b.isEmpty() || b==null)
return 0;
Collections.sort(a);
Collections.sort(b);
int result =Integer.MIN_VALUE;
for( int i =0; i<a.size(); i++)
{
result=Math.max(Math.abs(a.get(i)-b.get(i)),result);
}
return result;
}