-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindMedianSortedArrays.java
More file actions
75 lines (66 loc) · 2.23 KB
/
findMedianSortedArrays.java
File metadata and controls
75 lines (66 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.*;
class findMedianSortedArrays{
public static void main(String args[]){
int nums1[] = {1,2};
int nums2[] = {3,4};
System.out.println(FindMedianSortedArrays(nums1,nums2));
}
public static double FindMedianSortedArrays(int nums1[],int nums2[]){
List<Integer> num = new ArrayList<>();
int i = 0;
int j = 0;
while(i<nums1.length & j<nums2.length){
if(nums1[i]<=nums2[j]){
num.add(nums1[i]);
i++;
}else{
num.add(nums2[j]);
j++;
}
}
for(;i<nums1.length;i++) num.add(nums1[i]);
for(;j<nums2.length;j++) num.add(nums2[j]);
double result;
if(num.size() % 2 == 0){
int A = num.get(num.size()/2);
int B = num.get(num.size()/2-1);
result = (double)(A+B)/2;
return result;
}
int index = (int)Math.floor(num.size()/2);
result = num.get(index);
return result;
}
public static double FindMedianSortedArrays(int[] A, int[] B) {
int m = A.length;
int n = B.length;
if (m > n) { // to ensure m<=n
int[] temp = A; A = B; B = temp;
int tmp = m; m = n; n = tmp;
}
int iMin = 0, iMax = m, halfLen = (m + n + 1) / 2;
while (iMin <= iMax) {
int i = (iMin + iMax) / 2;
int j = halfLen - i;
if (i < iMax && B[j-1] > A[i]){
iMin = i + 1; // i is too small
}
else if (i > iMin && A[i-1] > B[j]) {
iMax = i - 1; // i is too big
}
else { // i is perfect
int maxLeft = 0;
if (i == 0) { maxLeft = B[j-1]; }
else if (j == 0) { maxLeft = A[i-1]; }
else { maxLeft = Math.max(A[i-1], B[j-1]); }
if ( (m + n) % 2 == 1 ) { return maxLeft; }
int minRight = 0;
if (i == m) { minRight = B[j]; }
else if (j == n) { minRight = A[i]; }
else { minRight = Math.min(B[j], A[i]); }
return (maxLeft + minRight) / 2.0;
}
}
return 0.0;
}
}