-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_4.java
More file actions
48 lines (44 loc) · 1.22 KB
/
Copy pathP_4.java
File metadata and controls
48 lines (44 loc) · 1.22 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
package leetcode.hard;
public class P_4 {
// log (2e6) * log(n * m)
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
final int n = nums1.length + nums2.length;
final int k1;
final int k2;
if (n % 2 == 0) {
k1 = (n / 2) - 1;
k2 = n / 2;
} else {
k1 = k2 = n / 2;
}
final int l = upperBound(nums1, nums2, k1);
final int r = upperBound(nums1, nums2, k2);
return 0.5 * (l + r);
}
private static int upperBound(int[] nums1, int[] nums2, int k) {
int lo = (int) -1e6;
int hi = (int) 1e6;
while (lo < hi) {
final int mid = lo + hi + 1 >> 1;
if (lowerBound(nums1, mid) + lowerBound(nums2, mid) > k) {
hi = mid - 1;
} else {
lo = mid;
}
}
return lo;
}
private static int lowerBound(int[] arr, int target) {
int lo = 0;
int hi = arr.length;
while (lo < hi) {
final int mid = lo + hi >>> 1;
if (arr[mid] < target) {
lo = mid + 1;
} else {
hi = mid;
}
}
return lo;
}
}