#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)
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); }
相关文章推荐
- (google面试题)找出无序数组中连接和最大排序
- 给两个排序好的数组,怎么求两个数组合并后的中位数?
- [leetcode] Median of Two Sorted Arrays 两个排序数组找出第k个数,或者中位数
- [各种面试题] 两个排序数组的中位数
- 找出两个已排序且同序大小相等的数组的中位数C语言
- 【LeetCode】004 Median of Two Sorted Arrays 两个排序数组合并后的中位数
- [LeetCode]4. Median of Two Sorted Arrays两个排序数组合并后的中位数
- 找出两个排序数组的中位数
- 给定两个有序的n长度的数组,如何找出这两个数组合并后的中位数?
- [程序员面试题精选100题]10.排序数组中和为给定值的两个数字
- 微软,Google面试题 (10) —— 在排序数组中寻找两个数的和等于给定数
- 面试题之2个有序数组求合并后的中位数
- Arithmetic problem | 求两个排序数组的中位数
- 两个排序数组的中位数
- Lintcode 两个排序数组的中位数
- 【算法】【分治】两个排序数组的中位数
- 4. Median of Two Sorted Arrays(两个排序数组的中位数)
- 面试题_找出数组中两个只出现一次的数字
- 面试题之2个有序数组求合并后的中位数
- 【leetcode】两个排序数组的中位数