See the passing rate of the problem is very surprised, feel this problem is very easy, because in fact, its thinking is very simple.
1) The dumbest way to do this is to use sorting to combine two arrays into an array, and then return the median, which should time out.
2) Use a merge-like operation to find the median, use two pointers to a and B array headers to iterate through the array, and then count the number of elements until the median is found, at which point the algorithm complexity is O (n).
The first thing I thought of was 2 this approach, but really write code, only to find that there are a lot of details to consider, very cumbersome.
3) Finally, a very good method was seen from medianof-sorted arrays. The original text is interpreted in English, where we translate it into Chinese. The core of this approach is to turn the original problem into a problem that looks for the K decimal (assuming that the two original sequence is in ascending order), so that the median is actually a small number (M+n)/2. So as long as the problem of k decimal is solved, the original problem is solved.
First, assuming that the number of elements of arrays A and b are greater than K/2, we compare a[k/2-1] and b[k/2-1] two elements, which represent the K/2 small element of a and the K/2 small element of B respectively. These two elements compare a total of three cases:>, < and =. If a[k/2-1]<b[k/2-1], this means that the elements of a[0] to a[k/2-1] are in the first K-small element after the merging of a and B. In other words, a[k/2-1] cannot be larger than the K-decimal value after the two array is merged, so we can discard it.
Proving is also very simple, can be used to disprove the law. Suppose A[k/2-1] is greater than the K-value after merging, we may as well assume that it is a small value (k+1). Because a[k/2-1] is less than b[k/2-1], b[k/2-1] is at least the (k+2) small value. But in fact, there are at most k/2-1 elements in a is less than a[k/2-1],b and there are at most k/2-1 elements less than a[k/2-1], so the number of elements less than a[k/2-1] is at most k/2+ k/2-2, less than K, this and a[k/2-1] is the first (k + 1) The number of contradictions.
When A[k/2-1]>b[k/2-1], a similar conclusion exists.
When A[k/2-1]=b[k/2-1], we have found the small number of K, which is the equal element, which we remember as M. Since there are k/2-1 elements in a and B are less than m, so M is the small number of K. (There may be some doubt that if K is an odd number, then M is not the median.) Here is the idealized consideration, slightly different in the actual code, is to seek K/2 first, and then use K-K/2 to get another number. )
Through the above analysis, we can use recursive method to find the number of small K. In addition we need to consider several boundary conditions:
- If A or B is empty, return directly to B[k-1] or a[k-1];
- If k is 1, we only need to return the smaller values in a[0] and b[0];
- If a[k/2-1]=b[k/2-1], return one of them;
Transferred from: http://blog.csdn.net/yutianzuijin/article/details/11499917/
The final implementation code is:
Median of Sorted Arrays