No. 004: Median of Two Sorted Arrays, No. 004median
Question:
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 )).
Example1:
Nums1 = [1, 3]
Nums2 = [2]
The median is 2.0
Example2:
Nums1 = [1, 2]
Nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
Official difficulty:
Hard
Translation:
The size of two sorted arrays is m and n, respectively, to find the median of the two arrays.
The time complexity of the entire algorithm is O (log (m + n ))
Example:
[1, 3] and [2]. The median value is 2.0.
[1, 2] and [3, 4], with a median of 2.5
Ideas:
1. at first glance, it is not difficult. Because the two arrays are sorted, the two arrays are merged into an ordered array, and the number size of the current pointer of the two arrays is compared each time. A small number is placed into the new array. After obtaining the result array, returns the median value. However, the time complexity is O (m + n ).
2. based on the idea in step 1, you do not need to retrieve the result array. You can use the result array to calculate the result at the point where the traversal is sent to the median. In addition, you can save a set of space. However, the time complexity is O (m + n)/s), which is still not up to standard.
3. take the median comparison of two ordered arrays, remove the array of the relatively small number, the value before the median, and the array of the relatively large number, the value after the median. Use recursion to use the remaining part of the array as the initial value of the next recursion. The time complexity is O (log (m + n )).
Possible difficulties in solving problems:
1. The size of the array removed each time must be the same. If it is different, it is closer to the one with a small length. For example: [, 3] and [, 8], the next recursive array should be [] and [,], instead of [2, 3] and [4, 5, 6].
2. when the length of the array is removed to 0, we need to consider two situations: 1 is an array with a length of 1; 2 is an array with a length of 2, in addition, the median value is relatively small.
3. In the above two cases, 2 is easier to consider, and 1 is divided into four cases, which will be clearly described in the Code.
4. Consider the case where an array is hollowed out.
Solution code:
1 // because the two arrays are ordered separately, the median is searched using the binary-like lookup method. The time complexity O (log (m + n) 2 private static double method3 (int [] array1, int [] array2) {3 // priority judgment, when an array is used up, 4 if (array1.length = 0 | array2.length = 0) {5 int [] tempArray; 6 if (array1.length = 0) {7 tempArray = array2; 8} else {9 tempArray = array2; 10} 11 // returns the median of a single ordered array, in parity, 12 return (tempArray [tempArray. length/2] + tempArray [(tempArray. length-1)/2])/ 2.0; 13} 14 // if both arrays exist, 15 int medianIndex1 = (array1.length-1)/2; 16 int medianIndex2 = (array2.length-1)/2; 17 // The operation array is assigned a uniform value of 18 int [] tempMedianMinArray; 19 int [] tempMedianMaxArray; 20 // The operator array median index marks 21 int minIndex; 22 int maxIndex; 23 // two arrays are assigned with a single value for processing 24 if (array1 [medianIndex1] <array2 [medianIndex2]) {25 tempMedianMinArray = array1; 26 tempMedianMaxArray = array2; 27 minIndex = medianIndex1; Ndex = medianIndex2; 29} else {30 tempMedianMinArray = array2; 31 tempMedianMaxArray = array1; 32 minIndex = medianIndex2; 33 maxIndex = medianIndex1; 34} 35 // The array to be removed is assigned 36 int [] arrayToReduceFromBegin = Arrays. copyOf (tempMedianMinArray, minIndex); 37 int [] arrayToReduceToEnd = Arrays. copyOfRange (tempMedianMaxArray, tempMedianMaxArray. length-maxIndex, 38 tempMedianMaxArray. length); 39 // The length of the array to be removed is not 040 I F (! (ArrayToReduceFromBegin. length = 0 | arrayToReduceToEnd. length = 0) {41 // The length of the array subtracted from the two arrays each time must be the same 42 int reduceLength = Math. min (arrayToReduceFromBegin. length, arrayToReduceToEnd. length); 43 int [] nextArray1 = Arrays. copyOfRange (tempMedianMinArray, reduceLength, tempMedianMinArray. length); 44 int [] nextArray2 = Arrays. copyOf (tempMedianMaxArray, tempMedianMaxArray. length-inclucelength); 45 return method3 (nextArray1, nextArray2); 46} else {47 // There are two situations: Case 1. an array with a length of 1 exists; Case 2. there is a side with a length of 2 and a smaller median of 48 // first consider case 2, including some cases 1 also applies to 49 if (tempMedianMinArray. length = 2) {50 // small array to the first 1 bit, large array to the last 1 bit 51 int [] nextArray1 = Arrays. copyOfRange (tempMedianMinArray, 1, 2); 52 int [] nextArray2 = Arrays. copyOf (tempMedianMaxArray, tempMedianMaxArray. length-1); 53 return method3 (nextArray1, nextArray2); 54} else {55 // In case of 156 // the length of both arrays is 1, special consideration 57 if (tempMedianMinArray. length = 1 & tempMedianMaxArray. length = 1) {58 return (tempMedianMinArray [0] + tempMedianMaxArray [0])/2.0; 59} 60 // The value of the unified operation is 61 int [] tempArray1; 62 int [] tempArray2; 63 if (tempMedianMinArray. length = 1) {64 tempArray1 = tempMedianMinArray; 65 tempArray2 = tempMedianMaxArray; 66} else {67 tempArray1 = tempMedianMaxArray; 68 tempArray2 = tempMedianMinArray; 69} 70 // then there are 4 cases 71 if (tempArray1 [0] <tempArray2 [tempArray2.length/2]) {72 if (tempArray1 [0] <tempArray2 [0]) {73 // For example, [1] and [6, 7, 8] 74 tempArray1 = new int [] {}; 75 // tail 76 tempArray2 = Arrays. copyOf (tempArray2, tempArray2.length-1); 77} else {78 // For example, [7] and [6, 8, 9] 79 tempArray2 = Arrays. copyOfRange (tempArray2, 1, tempArray2.length-1); 80} 81} else {82 if (tempArray1 [0]> tempArray2 [tempArray2.length-1]) {83 // For example, [9] and [6, 7, 8] 84 tempArray1 = new int [] {}; 85 // remove the header 86 tempArray2 = Arrays. copyOfRange (tempArray2, 1, tempArray2.length); 87} else {88 // For example, [8] and [6, 7, 9] 89 tempArray2 = Arrays. copyOfRange (tempArray2, 1, tempArray2.length-1); 90} 91} 92 return method3 (tempArray1, tempArray2); 93} 94}View Code
Test Code address:
Https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/hard/Q004.java
LeetCode address:
Https://leetcode.com/problems/median-of-two-sorted-arrays/
PS: in the source code, I mentioned in my thoughts that the first two methods, namely, the time complexity are O (m + n) and O (m + n)/2).
PPS: If you have any incorrect or more efficient methods, please leave a message. Thank you!