您的位置:首页 > 职场人生

#google面试题14#找出两个排序数组的合并后的中位数

2013-03-07 22:06 246 查看
given sorted int[] A, int[] B. How would you find the maiden that would have been if both were combined to one big sorted array? Use
divide and conqure recurssion.

please write method "GetMutualMaiden" and explaint it's time and space complexity

Assumption before solving the problem:

Definition of Median: if the size of array is odd, such as {1, 2, 3, 4, 5}, 3 is the median number; if the size of array is even, such as {1, 2, 3, 4, 5, 6}, I will assume that either 3 or 4 is median.

Basic Idea:

suppose we have two arrays:

A = {2, 4, 6, 8, 10}

B = {1, 3, 5, 7, 9, 11, 13}

We compare the two median number, Median(A) = 6, and Median(B) = 7.

since Median(A) < Median(B), we can remove the first half of A, that is {2, 4}, and the second half of B, that is {9, 11, 13}, because the mutual median number cannot be in these two parts. In order to keep the mutual median number unchanged, in this case,
we can remove {2, 4} and {11, 13}. As we remove two elements that are less than mutual median and two elements that are larger than mutual median, the original mutual median will remain same.

So that after removal, A and B changed to be:

A = {6, 8, 10}

B = {1, 3, 5, 7, 9}

We can continue using this method until we find the mutual median number, below is the implementation in Java (may have some small bugs in the code)


public static int getMutualMedian(int[] A, int[] B) {
// if A is an empty array, return the median of array B
if (A.length == 0) return getMedian(B, 0, B.length-1 );

// if B is an empty array, return the median of array A
if (B.length == 0) return getMedian(A, 0, A.length-1);

// calling for recursive helper function
return getMutualMedian(A, B, 0, A.length-1, 0, B.length-1);
}

// get the median number of one single array with specific range
public static int getMedian(int[] A, int start, int end) {
if (start > end) return 0;
int median = (start + end) / 2;
return A[median];
}

// get the median number of one single array and an extra integer
public static int getMedian(int a, int[] B, int startB, int endB) {
if (startB == endB) return a;
int median = (startB + endB) / 2;
if (a >= B[median]) return B[median+1];
return B[median];
}

// helper function to calculate the mutual median
public static int getMutualMedian(int[] A, int[] B, int startA,
int endA, int startB, int endB) {
//if (startA == endA && startB == endB) return A[startA];

// Base case: array A has only one element
if (startA == endA) return getMedian(A[startA], B, startB, endB);

// Base case: array B has only one element
if (startB == endB) return getMedian(B[startB], A, startA, endA);

int medianA = (startA + endA) / 2;
int medianB = (startB + endB) / 2;

// Special case: two median numbers are equal
if (A[medianA] == B[medianB]) return A[medianA];

int offset = 0;
if ((medianA - startA) >= (medianB - startB)) {
offset = endB - medianB;
} else {
offset = endA - medianA;
}

// update the range to search the median number
if (A[medianA] > B[medianB]) {
startB += offset;
endA -= offset;
} else {
startA += offset;
endB -= offset;
}
return getMutualMedian(A, B, startA, endA, startB, endB);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: