LeetCode [4]. Median of Two Sorted Arrays, leetcodemedian
Median of Two Sorted Arrays
This question is indeed a little difficult. It takes a long time to think about it and you cannot understand the answer from others. The question is Hard. The original question is as follows:
There are two sorted arrays nums1 and nums2 of size m and n respectively. find the median of the two sorted arrays. the overall run time complexity shoshould be O (log (m + n )).Given two sorted arrays, calculate the median of the two arrays. The time complexity must be O (log (m + n )). I. Idea: Adjust the median request of the ontology to merge and sort the two arrays and then obtain the k Number. The following is an example:
Figure 1. In the example, the number of students in Class A and Class B is aEnd and bEnd, respectively. One day, the PE teacher said that he was looking for the student whose number was k short, therefore, the two classes were first formed into two teams in the ascending order. Suppose k is 13. At this time, Class A sent the number x short students, and Class B sent the number y short students for comparison (Note: At this time, x + y <= k ). If x is 6, y is 7. If A's 6th students are shorter than B's 7th students, if the two classes are sorted as A whole, the six students in Class A must be in front of the 7th students in Class B, A maximum of 6th persons in Class A stand in the first place of Class B's 7th, and A maximum of 12th in the overall ranking. So we can discard the first six digits of Class A and find them in the remaining two queues. But it is no longer looking for the k number, but for the k-x number. Because x's base number is lost. Of course, in the above example, as long as (x + y) <= k, x and y are non-negative. For efficiency, x = y = (k/2) is generally used ). When a team is lost to few or no one left, the value must be set based on the actual situation. Ii. 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, Constantly narrowing the range of the two arrays, at the same time, the value of k also changes public double find (int [] A, int aStart, int aEnd, int [] relative to the upper limit of the two search zones. B, int bStart, int bEnd, int kth) {// 1. place the shorter values in the preceding items of the find function parameter if (aEnd> bEnd) return find (B, bStart, bEnd, A, aStart, aEnd, kth ); // 2. if the length is short, the answer is equivalent to finding the median of another array if (aEnd <= 0) return B [bStart + kth-1]; // 3. recursion to the end, The aStart and bStart of the two arrays 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, bS Tart, 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: ;}}
Reference: LeetCode-Median of Two Sorted Arrays Java Median of Two Sorted Arrays (JAVA)