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

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);
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值