4. Find two ordered arrays of median (log (min (n, m))) to achieve

Looking for a median of two ordered arrays

Given two ordered arrays of size and nums1 m and n nums2.

Please find both the median and orderly array, and requires time complexity of the algorithm is O (log (m + n)).

You can not assume nums1 and nums2 both empty.

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0
Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3) / 2 = 2.5

 

To solve this problem, we need to understand "what is the role of the median." In statistics, the median is used to:

One set into two subsets of equal length, wherein a subset of the elements are always larger than the other elements in the subset.

Which is divided into odd and even numbered groups:

Odd: [235] corresponding to a median of 3

Even-numbered groups: [1479] corresponding to a median of (4 + 7) / 2 = 5.5

 

First explain "cut"
We cut back to an ordered array can be divided into two parts left and right, cut the knife that was called cut (Cut), about cutting (Cut) will have two elements, which are left maximum and minimum values on the right.

We define LMax = Max (LeftPart), RMin = Min (RightPart).

Cut two numbers can be cut in the middle, may be cut in a number of 1, if cutting on a number, then this number i.e. belongs to the left, also belong to the right

Odd: [235] corresponding to a median of 3, assuming cut (Cut) on 3, we can be divided into two 3: [2 (3/3) 5]

Thus LMax = 3, RMin = 3

Even-numbered groups: [1479] corresponding to a median of (4 + 7) / 2 = 5.5, assuming cut (Cut) between 4 and 7: [1 (4/7) 9]

Thus LMax = 4, RMin = 7

And cutting the k-th element of
an array
to an ordered array, for arrays A, if the cut (Cut) at position k bit (not cut (Cut) in the middle two numbers), then LMax = RMin = A [k] ,

Two arrays
that is the subject of our state, we have requirements when the two arrays are combined into an ordered array, the first element of k bits

We set:
Ci is to cut the i-th array.

LMaxi after the i-th array element cut left.

RMini i-th array element of a right-cutting. 

 

 

First, LMax1 <= RMin1, LMax2 <= RMin2 that's for sure, because the array is ordered, certainly less than the left to the right!, But if the cut (Cut) on a number, then about equal.

Secondly, if we let LMax1 <= RMin2, LMax2 <= RMin1 it

 

 

 

So if the whole is less than the right half of the left half, if the number of elements on the left is just the sum equal to k, then the k-th element is Max (LMax1, LMax2), this is better understood, because Max (LMax1, LMax2) certainly left k elements of the maximum value because the combined arrays are ordered, the k-th element of certainty k elements in front of the largest one.

So if LMax1> RMin2, described left element of the array 1 is too large (multi), we reduced the C1, C2 = k-C1 is correspondingly increased. LMax2> RMin1 Similarly, reducing the C2, C1 = k-C2 is correspondingly increased.

Assuming k = 3

for

[2 3 5]

[1479]
setting C1 = 1, then C2 = k - C1 = 2

[2 / 3 5]

[1 4 / 7 9]

In this case LMax1 = 2, RMin1 = 3, LMax2 = 4, RMin2 = 7,

Thus LMax2> RMin1, under the previous reasoning, we want to C1 increases, so we let C1 = 2, as follows:

[2 3 /5]

[1 / 4 7 9]

In this case LMax1 = 3, RMin1 = 5, LMax2 = 1, RMin2 = 4, satisfying LMax1 <RMin2 and LMax2 <RMin1, so that the third element is Max (LMax1, LMax2) = 3

The biggest problem is that two arrays, after their merger, m + n total number may be odd, it is even possible, so we have to make the idea of ​​m + n is always an even number

By the addition of virtual '#', we let converted into m + 1, n 2m converted into 2n + 1, and the two numbers becomes 2m + 2n + 2, constant even.

Note that virtual plus, in fact, did not this step, the following conversion, we can ensure that each element of the virtual plus-one correspondence with the original elements

 

 

 

 

After such a virtual plus, each location can be obtained by the original position of the element / 2:

2 For example, in the original bit 0, bit 1 is now 1/2 = 0

3 for example, in an original, it is now 3, 3/2 = 1

5 example, the two original, now 5, 5/2 = 2

9 for example, in the original 3 it is now 7, 7/2 = 3

For cut (Cut), if the cut in the '#' is equal between the two cutting elements cut in the number is equal to the number designated portion 2, there are always the following hold:

LMaxi = (Ci-1) on the element / the second position
RMini = Ci elements on / 2 position

E.g:

Cut in 3, C = 3, LMax = a [(3-1) / 2] = A [1], RMin = a [3/2] = A [1], the position is just 3!

Cutting between 4/7 '#', C = 4, LMax = A [(4-1) / 2] = A [1] = 4, RMin = A [4/2] = A [2] = 7

The rest of the things will be easier, the two arrays seen as a virtual array of A, A has 2m + 2n + 2 elements, cut at m + n + 1, so we just need to find the m + n + 1 elements and m + n + element position of the second position on the line.

Left: A [m + n + 1] = Max (LMax1, LMax2)

The right: A [m + n + 2] = Min (RMin1, RMin2)

==>Mid = (A[m+n+1]+A[m+n+2])/2 = (Max(LMax1,LMax2) + Min(RMin1,RMin2) )/2

The fastest cut (Cut) is the use of dichotomy,

There are two arrays, which we do half of it?
According to previous analysis, we know, as long as C1 or C2 is determined, the other also determined. Here, for efficiency, we certainly do is choose the length of the short half is assumed to be C1.

LMax1> RMin2, reducing the C1, C2 increases. -> C1 left half

LMax2> RMin1, increases the C1, C2 decreases. -> C1-half right

If C1 or C2 had simply run out how to do?

This occurs: If there is a full array of smaller or greater than the median. Assume that n <m, there may be four cases:

C1 = 0 - 1 as a whole are on the right array, so the value is greater than the median in the array 2, simply means that the left side of the array 1 cut is empty, so we can assume that LMax1 = INT_MIN

C1 = 2n - 1 whole array are left, so the value is less than the median in the array 2, simply means that the right side of the array 1 cut is empty, so we can assume that RMin1 = INT_MAX, to ensure LMax2 <RMin1 permanent establishment

C2 = 0-- whole array 2 on the right, so the value is greater than the median in the array 1, simply means that the left side of the array 2 cut is empty, so we can assume that LMax2 = INT_MIN

C2 = 2m-- whole array 2 on the left, so the value is less than the median in the array 1, simply means that the right side of the array 2 cut is empty, in order to allow LMax1 <RMin2 permanent establishment, we can assume RMin2 = INT_MA

Subject to answer

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 class Solution {
 5 public:
 6     double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
 7         int n = nums1.size();
 8         int m = nums2.size();
 9         if (n > m){
10             return findMedianSortedArrays(nums2, nums1);
11         }
12         int LMax1, LMax2, RMin1, RMin2, c1, c2, lo = 0 , Hi = 2 * n-;
 13 is          the while (LO <= Hi) {
 14              C1 = (LO + Hi) / 2 ;   // C1 dichotomous outcome 
15              C2 = m + n-- C1;
 16              LMax1 = (C1 = = 0 ) INT_MIN:? nums1 [(C1 - . 1 ) / 2 ];
 . 17              Rmin1 = (C1 == 2 * n-) INT_MAX: nums1 [C1 /? 2 ];
 18 is              Lmax2 = (C2 == 0 ) INT_MIN:? nums2 [(C2 - . 1 ) / 2 ];
 . 19             RMin2 = (c2 == 2 * m) ? INT_MAX : nums2[c2 / 2];
20             cout << LMax1<<" "<<RMin1<<endl;
21             cout << LMax2<<" "<<RMin2<<endl;
22             if (LMax1 > RMin2)
23                 hi = c1 - 1;
24             else if (LMax2 > RMin1)
25                 lo = c1 + 1
26 ;            else
27                 break;
28         }
29         return (max(LMax1, LMax2) + min(RMin1, RMin2)) / 2.0;
30     }
31 };
32 
33 int main(){
34     vector<int> a = {1,2,3};
35     vector<int> b = {4,5,6,7};
36     Solution su;
37     cout << su.findMedianSortedArrays(a,b) << endl;
38     return 0;
39 }

 

Transfer solution to a problem https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/4-xun-zhao-liang-ge-you-xu-shu-zu-de-zhong-wei- shu /

Guess you like

Origin www.cnblogs.com/zllwxm123/p/11440917.html