|
5 | 5 | * Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
|
6 | 6 | *
|
7 | 7 | * Solution
|
8 | | - * Take minimum size of two array. Possible number of cuts are from 0 to m in m size array. |
| 8 | + * Take minimum size of two array. Possible number of partitions are from 0 to m in m size array. |
9 | 9 | * Try every cut in binary search way. When you cut first array at i then you cut second array at (m + n + 1)/2 - i
|
10 | | - * Now try to find the i where a[i-1] <= b[j] and b[j-1] <= a[i]. So this i is the answerr |
| 10 | + * Now try to find the i where a[i-1] <= b[j] and b[j-1] <= a[i]. So this i is partition around which lies the median. |
11 | 11 | *
|
12 | | - * Time complexity is O(log(min(m,n)) |
| 12 | + * Time complexity is O(log(min(x,y)) |
| 13 | + * Space complexity is O(1) |
13 | 14 | *
|
14 | 15 | * https://leetcode.com/problems/median-of-two-sorted-arrays/
|
15 | 16 | * https://discuss.leetcode.com/topic/4996/share-my-o-log-min-m-n-solution-with-explanation/4
|
16 | 17 | */
|
17 | 18 | public class MedianOfTwoSortedArrayOfDifferentLength {
|
| 19 | + |
18 | 20 | public double findMedianSortedArrays(int input1[], int input2[]) {
|
| 21 | + //if input1 length is greater than switch them so that input1 is smaller than input2. |
19 | 22 | if (input1.length > input2.length) {
|
20 | 23 | return findMedianSortedArrays(input2, input1);
|
21 | 24 | }
|
22 | | - if (input1.length == 0) { |
23 | | - return getMedian(input2); |
24 | | - } |
25 | | - int m = input1.length; |
26 | | - int n = input2.length; |
| 25 | + int x = input1.length; |
| 26 | + int y = input2.length; |
27 | 27 |
|
28 | 28 | int low = 0;
|
29 | | - int high = m; |
| 29 | + int high = x; |
30 | 30 | while (low <= high) {
|
31 | | - int cut1 = (low + high)/2; |
32 | | - int cut2 = (m + n + 1)/2 - cut1; |
| 31 | + int partitionX = (low + high)/2; |
| 32 | + int partitionY = (x + y + 1)/2 - partitionX; |
33 | 33 |
|
34 | | - int a1 = (cut1 == 0) ? Integer.MIN_VALUE : input1[cut1 - 1]; |
35 | | - int a2 = (cut1 == m) ? Integer.MAX_VALUE : input1[cut1]; |
| 34 | + //if partitionX is 0 it means nothing is there on left side. Use -INF for maxLeftX |
| 35 | + //if partitionX is length of input then there is nothing on right side. Use +INF for minRightX |
| 36 | + int maxLeftX = (partitionX == 0) ? Integer.MIN_VALUE : input1[partitionX - 1]; |
| 37 | + int minRightX = (partitionX == x) ? Integer.MAX_VALUE : input1[partitionX]; |
36 | 38 |
|
37 | | - int b1 = (cut2 == 0) ? Integer.MIN_VALUE : input2[cut2 - 1]; |
38 | | - int b2 = (cut2 == n) ? Integer.MAX_VALUE : input2[cut2]; |
| 39 | + int maxLeftY = (partitionY == 0) ? Integer.MIN_VALUE : input2[partitionY - 1]; |
| 40 | + int minRightY = (partitionY == y) ? Integer.MAX_VALUE : input2[partitionY]; |
39 | 41 |
|
40 | | - if (a1 <= b2 && b1 <= a2) { |
41 | | - if ((m + n) % 2 == 0) { |
42 | | - return ((double)Math.max(a1, b1) + Math.min(a2, b2))/2; |
| 42 | + if (maxLeftX <= minRightY && maxLeftY <= minRightX) { |
| 43 | + //We have partitioned array at correct place |
| 44 | + // Now get max of left elements and min of right elements to get the median in case of even length combined array size |
| 45 | + // or get max of left for odd length combined array size. |
| 46 | + if ((x + y) % 2 == 0) { |
| 47 | + return ((double)Math.max(maxLeftX, maxLeftY) + Math.min(minRightX, minRightY))/2; |
43 | 48 | } else {
|
44 | | - return (double)Math.max(a1, b1); |
| 49 | + return (double)Math.max(maxLeftX, maxLeftY); |
45 | 50 | }
|
46 | | - } else if (a1 > b2) { |
47 | | - high = cut1 - 1; |
48 | | - } else { |
49 | | - low = cut1 + 1; |
| 51 | + } else if (maxLeftX > minRightY) {//we are too far on right side for partitionX. Go on left side. |
| 52 | + high = partitionX - 1; |
| 53 | + } else {//we are too far on left side for partitionX. Go on right side. |
| 54 | + low = partitionX + 1; |
50 | 55 | }
|
51 | 56 | }
|
| 57 | + |
| 58 | + //Only we we can come here is if input arrays were not sorted. Throw in that scenario. |
52 | 59 | throw new IllegalArgumentException();
|
53 | 60 | }
|
54 | 61 |
|
55 | | - privatedoublegetMedian(intarr[]){ |
56 | | - if(arr.length % 2 == 0){ |
57 | | - return ((double)arr[arr.length/2 - 1] + arr[arr.length/2])/2; |
58 | | - } else { |
59 | | - returnarr[arr.length/2]; |
60 | | - } |
| 62 | + publicstaticvoidmain(String[] args) { |
| 63 | + int[] x = {1, 3, 8, 9, 15}; |
| 64 | + int[] y = {7, 11, 19, 21, 18, 25}; |
| 65 | + |
| 66 | + MedianOfTwoSortedArrayOfDifferentLengthmm = newMedianOfTwoSortedArrayOfDifferentLength(); |
| 67 | + mm.findMedianSortedArrays(x, y); |
61 | 68 | }
|
62 | 69 | }
|
0 commit comments