Leetcode "4". Median of the different methods for Sorted Arrays--java

Source: Internet
Author: User

Median of Sorted Arrays


This problem is really a bit difficult, think for a long time, see other people's answer is not to understand all of a sudden. The problem is hard , the original title is as follows: there is the sorted arrays  nums1  AND  nums2  of size m and n respectively. Find The median of the sorted arrays. The overall run time complexity should be O (log (M+n)). For a given two sorted arrays, find the median of the whole two arrays. The time complexity requirement is O (log (m+n)). first, the idea:The requirement for the median of the ontology is adjusted to the number of K when the total of two arrays is sorted. Here is an example to illustrate:
figure I, exampleClass A and Class B students are aend and bend, a day sports teacher said to find them in the middle of the K short classmate, so first will be two classes of students according to the order from small to large to stand two teams. Suppose K is for 13. At this time a class sent out the first x short classmate, B class sent to the lower Y students to compare (note: At this time x+y<=k). if X is 6,y is 7. If the 6th student of A is shorter than the 7th classmate of B, that if the two classes in the overall order, that Class A 6 students must be standing in the Class B 7th, up to a class 6th standing in the Class B 7th, up to the whole row of 12th, how can not turn his platoon first k=13. So you can put the Class A top 6 lost, in the remaining two queues inside to find. But it is no longer to find the first K, but to find the first k-x. Because X did the cardinality was lost. of course, the above example, as long as (x+y) <=k the case, X and y as long as is non-negative, that can be. Just for the sake of efficiency, generally go to x=y= (K/2). When a team is left with little or no people, the value is based on the specific circumstances. Second, Java program:
public class Solution {public double findmediansortedarrays (int[] nums1, int[] nums2) {int m = nums1.length;        int n = nums2.length;                int k = m + N;        if ((k&1) ==1) {return find (nums1, 0, M, nums2, 0, N, k/2+1);    }else return (Find (nums1, 0, M, nums2, 0, N, k/2) +find (nums1, 0, M, nums2, 0, N, k/2+1)/2; }//recursive algorithm, shrinking the range of two arrays, and also changing the value of K relative to the upper starting point of the two search interval public double find (int[] A, int astart, int aend, int[] B, int bstart, I NT Bend,int kth) {//1. Unify the short length placed in the previous item of the find function parameter if (aend>bend) return find (B, bstart, bEnd, a, a            Start, Aend, KTH); 2.            Short length is empty, then the answer is equivalent to finding the median of another array if (aend<=0) return B[bstart + kth-1]; 3.                Recursively to the end point, two arrays of Astart and bstart have reached the median position if (kth==1) return min (A[astart],b[bstart]);                int pa = min (kth/2,aend), PB = Kth-pa; if (A[astart + pa-1]< B[bstart +pb-1]) return find (A, asTart+pa, Aend-pa, B, bstart, bEnd, KTH-PA);                    else return find (A, Astart, Aend, B, bstart + pb, BEND-PB,KTH-PB);    } public int min (int a, int b) {return a>b?b:a; }}




Reference:Leetcode–median of Sorted Arrays Java Median of Sorted Arrays (JAVA)

Leetcode "4". Median of the different methods for Sorted Arrays--java

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.