标签:
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
假设两个数组总共有n个元素,那么显然我们有用O(n)时间和O(n)空间的方法:用merge sort的思路排序,排序好的数组取出下标为k-1的元素就是我们需要的答案。
1 public class MedianofTwoSortedArrays2 { 2 //只不过不符合算法要求,时间复杂度在于排序的 n*log(n) 3 public static double findMedianLow(int A[], int B[]) { 4 int[] sumArray = ArrayUtils.addAll(A, B); 5 Arrays.sort(sumArray); 6 int length = sumArray.length; 7 if (length % 2 == 0) { 8 double num1 = sumArray[length / 2]; 9 double num2 = sumArray[length / 2 - 1]; 10 return (num1 + num2) / 2; 11 } else { 12 return sumArray[length / 2]; 13 } 14 }
最后从medianof two sorted arrays中看到了一种非常好的方法。原文用英文进行解释,在此我们将其翻译成汉语。该方法的核心是将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数。所以只要解决了第k小数的问题,原问题也得以解决。
首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]<B[k/2-1],这表示A[0]到A[k/2-1]的元素都在A和B合并之后的前k小的元素中。换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
证明也很简单,可以采用反证法。假设A[k/2-1]大于合并之后的第k小值,我们不妨假定其为第(k+1)小值。由于A[k/2-1]小于B[k/2-1],所以B[k/2-1]至少是第(k+2)小值。但实际上,在A中至多存在k/2-1个元素小于A[k/2-1],B中也至多存在k/2-1个元素小于A[k/2-1],所以小于A[k/2-1]的元素个数至多有k/2+ k/2-2,小于k,这与A[k/2-1]是第(k+1)的数矛盾。
当A[k/2-1]>B[k/2-1]时存在类似的结论。
当A[k/2-1]=B[k/2-1]时,我们已经找到了第k小的数,也即这个相等的元素,我们将其记为m。由于在A和B中分别有k/2-1个元素小于m,所以m即是第k小的数。(这里可能有人会有疑问,如果k为奇数,则m不是中位数。这里是进行了理想化考虑,在实际代码中略有不同,是先求k/2,然后利用k-k/2获得另一个数。)
通过上面的分析,我们即可以采用递归的方式实现寻找第k小的数。此外我们还需要考虑几个边界条件:
代码如下:
1 public static double findMedianSortedArrays(int A[], int B[]) { 2 int m = A.length; 3 int n = B.length; 4 int total = m + n; 5 //长度为积数取中间,为偶数去中间两个的平均值 6 if ((total & 0x01) != 0) { 7 return findMedian(A, m, B, n, total / 2 + 1); 8 } else { 9 return (findMedian(A, m, B, n, total / 2) + findMedian(A, m, B, n, 10 total / 2 + 1)) / 2.0; 11 } 12 } 13 14 //二分法,每次都能去除掉一部分范围外数据。需要注意每次去除数据都会改变数组的结构,所以需要特殊处理临界值 15 private static double findMedian(int A[], int m, int B[], int n, int target) { 16 if (m == 0) { 17 return B[target - 1]; 18 } else if (n == 0) { 19 return A[target - 1]; 20 } else if (target == 1) { 21 return A[0] < B[0] ? A[0] : B[0]; 22 } 23 int temp = target / 2; 24 if (Math.min(m, n) < temp) { 25 temp = Math.min(m, n); 26 } 27 if (A[temp - 1] > B[temp - 1]) { 28 return findMedian(A, m, Arrays.copyOfRange(B, temp, n), n - temp, target - temp); 29 } else if (A[temp - 1] < B[temp - 1]) { 30 return findMedian(Arrays.copyOfRange(A, temp, m), m - temp, B, n, target - temp); 31 } else { 32 return A[temp - 1]; 33 } 34 }
1 public class Solution { 2 3 public static int[] C; 4 5 public static void quickSort(int a[],int low, int high){ 6 if(low < high){ 7 int i = low; 8 int tmp; 9 for(int j = low; j< high; j++){ 10 if(a[j]<=a[high]){ 11 tmp = a[i]; 12 a[i] = a[j]; 13 a[j] = tmp; 14 ++ i; 15 } 16 } 17 tmp = a[i]; 18 a[i] = a[high]; 19 a[high] = tmp; 20 quickSort(a,low,i-1); 21 quickSort(a,i+1,high); 22 } 23 } 24 25 public static double findMedianSortedArrays(int A[], int B[]) { 26 double i ; 27 //combine these two arrays into a new array C 28 int l = A.length+B.length; 29 C = new int[l]; 30 System.arraycopy(A, 0, C, 0, A.length); 31 System.arraycopy(B, 0, C, A.length, B.length); 32 33 quickSort(C,0,l-1); 34 35 //for(int ii = 0; ii < l; ii ++) 36 //System.out.println(C[ii]); 37 38 if(l%2 == 0) 39 i = (double) ((C[l/2]+C[l/2-1])/2.0); 40 else 41 i = (double) C[(l-1)/2]; 42 43 return i; 44 } 45 }
LeetCode 4 -- Median of Two Sorted Arrays
标签:
原文地址:http://www.cnblogs.com/myshuangwaiwai/p/4470810.html