-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedian_twosorted_array_4leetcode.java
37 lines (31 loc) · 1.38 KB
/
median_twosorted_array_4leetcode.java
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
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if(nums1.length>nums2.length){
return findMedianSortedArrays(nums2,nums1);
}
int lengthA = nums1.length;
int lengthB = nums2.length;
int low = 0;
int high =lengthA;
while(low<=high){
int partitionA = (low+high)/2;
int partitionB = (lengthA + lengthB +1)/2 - partitionA;
int maxLeftA = partitionA == 0 ? Integer.MIN_VALUE : nums1[partitionA - 1];
int minRightA = partitionA == lengthA ? Integer.MAX_VALUE : nums1[partitionA];
int maxLeftB = partitionB == 0 ? Integer.MIN_VALUE : nums2[partitionB - 1];
int minRightB = partitionB == lengthB ? Integer.MAX_VALUE : nums2[partitionB];
if(maxLeftA <= minRightB && maxLeftB <= minRightA){
if((lengthA + lengthB) % 2 == 0){
return ((double)Math.max(maxLeftA, maxLeftB) + Math.min(minRightA, minRightB))/2;
}else{
return ((double)Math.max(maxLeftA, maxLeftB));
}
}else if(maxLeftA>minRightB){
high = partitionA -1;
}else{
low = partitionA +1;
}
}
throw new IllegalArgumentException();
}
}