There is sorted arrays A and B of size m and N respectively. Find The median of the sorted arrays. The overall run time complexity should be O (log (m+n)).
Analysis
This is a very classic question. The more general form of the problem is that given two sorted arrays, find the elements of the K-large element in each of the two elements. The solution of O (M + N) is straightforward, merge two arrays directly, and then seek the elements of the K-large.
But we just need the K-big element, which does not require the "sort" to be so complicated. You can use a counter to record the elements that have already been found in the big M. At the same time we use two pointers PA and PB, respectively, to the first element of the A and B arrays, using a principle similar to merge sort, if the current element of array A is small, then pa++, and m++, if the current element of array B is small, then pb++, and m++. Finally, when M equals K, we get our answer, O (k) time, O (1) space. However, when K is close to M + N, this method is still O (M + N).
Is there a better plan? We can consider starting with K. If we are able to delete an element that is certain to precede the K element, then we need to do it k times. But what if we delete half of it every time? Since both a and B are orderly, we should make full use of the information in this, similar to binary search, but also make full use of the "orderly". Assuming that the number of elements a and B is greater than K/2, we compare the first K/2 element of a (i.e. a[k/2-1]) with the K/2 element of B (i.e. b[k/2-1]) in the following three cases (to simplify the hypothesis that K is even, the result is
For k is odd is also established):
? A[K/2-1] = = b[k/2-1]
? A[K/2-1] > B[k/2-1]
? A[K/2-1] < b[k/2-1]
If a[k/2-1] < b[k/2-1], meaning a[0] to a[k/2-1 is definitely in the range of a [B's top k element
Inside, in other words, a[k/2-1 cannot be larger than the K-element of a [B]. Left to the reader to prove.
Therefore, we can safely delete this K/2 element of the A array. Similarly, when a[k/2-1] > b[k/2-1], you can
To delete the K/2 elements of the B array.
When a[k/2-1] = = b[k/2-1], the description found the element k large, directly return to a[k/2-1] or b[k/2-1]
Can.
Therefore, we can write a recursive function. So when should the function terminate?
? When a or B is empty, return directly to B[k-1] or a[k-1];
? When K=1 is, return min (a[0], b[0]);
? When a[k/2-1] = = b[k/2-1], return a[k/2-1] or b[k/2-1]
Class Solution {public:double findmediansortedarrays (int a[], int m, int b[], int n) {int total = m + n;if (Total & 0x 1) return find_kth (A, m, B, N, TOTAL/2 + 1); Elsereturn (Find_kth (A, m, B, N, TOTAL/2) + find_kth (A, m, B, N, TOTAL/2 + 1))/2.0;} private:static int find_kth (int a[], int m, int b[], int n, int k) {//always assume that M is equal or smaller than NIF (m > N) return find_kth (B, N, A, M, k); if (M = = 0) return b[k-1];if (k = = 1) return min (a[0], b[0]);//divide K into Partsint ia = min (K/2, m), IB = K-ia;if (A[ia-1] < b[ib-1]) return find_kth (A + ia, M-ia, B, N, K-ia); else if (A[ia-1] > B[ib-1]) return find_kth (A, M, B + IB, N-ib, K-ib); Elsereturn a[ia-1];}};
Leetcode----------------Median of Sorted Arrays