LeetCode4. Median of the Sorted Arrays---vector implementation O (log (m+n)---findkth

Source: Internet
Author: User

The question is the same as 1029 on PAT. But PAT1029 with O (m+n) The time complexity (the problem) can be, this question request is O (log (m+n)).

The problem took me a workday to think about. Because it is log, I have been thinking about how to make two points, thinking that two vectors are two separate, but never think of the right solution, is infinite if else

Later read the puzzle. The idea is to look for the small number of K in the two ordered arrays.

<1> spent a long time in the debugging, the results found that there is less a return, during the constant suspicion of their ability to write recursion ... To be defeated by oneself, tune code is really a small but very expensive thing.

<2> the use of vectors is also drunk.

Ideas are as follows:

We first assume that the number of elements in the nums1 and nums2 arrays is greater than K/2, and numbering starts with 0, then we compare a = nums1[K/2-1] and B = Nums2[K/2 -1].

(1) If a < b then nums1[0] to nums1[K/2-1] This K/2 number in the merged ordered array, must be on the left side of the number of small K. Why is it?

We found that the number ofnums1 in the array is K/2-1 altogether. The nums2 array has a maximum of K/2-1 in the number of smaller than a. Thus the number of K/2-1 + K/2-1 < k-2 is the largest after merging.

That is, a is a maximum of k-1 small number. So the nums1 array before the K/2 number can be removed.

(2) If a > b is the same, the number of K/2 before the nums2 array is removed.

(3) If a = B,a is the request.

The time complexity is O (LOGK) for this problem, k = (m+n)/2 So the time complexity is O (log (m+n)), each time a half element of K is removed

Consider the actual situation:

If the number of elements in the nums1 and nums2 arrays is less than K/2, in general, we only need to meet the total number of the preceding two is k, the principle and the above principle is similar, no longer repeat.

In addition, special cases need to be considered,

(1) An array is empty, then put back to the other array of the K large;

(2) The k==1 directly returns min (Nums1[0],nums2[0]).

When actually implemented, because the input is a vector, it is a dynamic array, you can add or delete elements in real time, but cannot specify the starting position and the end of the same as the normal array/pointer, so we have to use the delete operation

Erase (ITER1,ITER2), removes the element between [Iter1,iter2].

1 classSolution {2  Public:3     DoubleFindkth (vector<int>& nums1,vector<int>& Nums2,intk)4     {5         intm = Nums1.size (), n =nums2.size ();6         if(M >N)7             returnfindkth (nums2,nums1,k); //error 1. Forget the "return";  8         if(M = =0)9             return Double(Nums2[k-1]); //error 2. Write as nums2[n-1];  Ten         if(k = =1) One         { A             return Double(Min (nums1[0],nums2[0])); -         } -         intpa = min (k/2, m), Pb = K-PA; the         if(Nums1[pa-1] < NUMS2[PB-1]) -         { -Nums1.erase (Nums1.begin (), Nums1.begin () +PA); -             returnFindkth (Nums1,nums2,k-PA); +         } -         Else if(Nums1[pa-1] > NUMS2[PB-1]) +         { ANums2.erase (Nums2.begin (), Nums2.begin () +pb); at             returnFindkth (Nums1,nums2,k-pb); -         } -         Else -             return Double(Nums1[pa-1]); -     } -     DoubleFindmediansortedarrays (vector<int>& Nums1, vector<int>&nums2) { in         intLen1 = Nums1.size (), len2 =nums2.size (); -         intLen = len1 +Len2; tovector<int>Cop1 (NUMS1), COP2 (NUMS2); +         if(Len%2) -         { the             returnFindkth (Nums1,nums2,len/2+1); *         } $         ElsePanax Notoginseng         { -             DoubleT1=findkth (Nums1,nums2,len/2), t2=findkth (Cop1,cop2,len/2+1); the             return(t1 + T2)/2; +         } A     } the};

Removing the elements of the vector requires ① an extra backup of the array, ② the delete operation. Needlessly time-consuming, and thus re-implemented as the starting position of the manual tag array. Only on the basis of the implementation method above + star can be, the principle is the same.

1 classSolution {2  Public:3     DoubleFindkth (vector<int>& Nums1,intST1,inted1,vector<int>& Nums2,intSt2,intED2,intk)4     {5         intm = Ed1-st1,n = Ed2-St2;6         if(M >N)7             returnFindkth (NUMS2,ST2,ED2,NUMS1,ST1,ED1,K);//error 1. Forget the "return";8         if(M = =0)9             return Double(Nums2[st2 + K-1]);//error 2. Write as nums2[n-1];Ten         if(k = =1) One         { A             return Double(min (nums1[st1],nums2[st2])); -         } -         intpa = min (k/2, m), Pb = K-PA; the         if(Nums1[st1 + PA-1] < Nums2[st2 + PB-1]) -         {  -             returnFindkth (Nums1,st1 + pa,ed1,nums2,st2,ed2,k-PA); -         } +         Else if(Nums1[st1 + PA-1] > nums2[st2 + PB-1])//error 3. Forget the "St2 +"; -         { +             returnFindkth (Nums1,st1,ed1,nums2,st2 + pb,ed2,k-pb); A         } at         Else -             return Double(Nums1[st1 + PA-1]); -     } -     DoubleFindmediansortedarrays (vector<int>& Nums1, vector<int>&nums2) { -         intLen1 = Nums1.size (), len2 =nums2.size (); -         intLen = len1 +Len2; in         if(Len%2) -         { to             returnFindkth (NUMS1,0, LEN1,NUMS2,0, Len2,len/2+1); +         } -         Else the         { *             DoubleT1 = findkth (NUMS1,0, LEN1,NUMS2,0, Len2,len/2), t2 = findkth (nums1,0, LEN1,NUMS2,0, Len2,len/2+1); $             return(t1 + T2)/2;Panax Notoginseng         } -     } the};

LeetCode4. Median of the Sorted Arrays---vector implementation O (log (m+n)---findkth

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.