-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedianOfTwoSortedArrays.java
More file actions
55 lines (44 loc) · 1.75 KB
/
MedianOfTwoSortedArrays.java
File metadata and controls
55 lines (44 loc) · 1.75 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
/**
* Leet code problem #4. Median of Two Sorted Arrays
* https://leetcode.com/problems/median-of-two-sorted-arrays/
*
* There are two sorted arrays nums1 and nums2 of size m and n respectively.
* Time complexity: O(log(min(m, n)))
* Space complexity: O(1)
*/
public class MedianOfTwoSortedArrays {
public static void main(String[] args) {
int[] nums1 = { 1, 3 };
int[] nums2 = { 2, 4 };
double median = findMedianSortedArrays(nums1, nums2);
System.out.println("Median: " + median);
}
public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
if (nums1.length > nums2.length) {
return findMedianSortedArrays(nums2, nums1);
}
int m = nums1.length;
int n = nums2.length;
int start = 0;
int end = m;
while (start <= end) {
int partition1 = (start + end) / 2;
int partition2 = (m + n + 1) / 2 - partition1;
int minNums1 = (partition1 == 0) ? Integer.MIN_VALUE : nums1[partition1 - 1];
int maxNums1 = (partition1 == m) ? Integer.MAX_VALUE : nums1[partition1];
int minNums2 = (partition2 == 0) ? Integer.MIN_VALUE : nums2[partition2 - 1];
int maxNums2 = (partition2 == n) ? Integer.MAX_VALUE : nums2[partition2];
if (minNums1 <= maxNums2 && minNums2 <= maxNums1) {
if ((m + n) % 2 == 0) {
return (Math.max(minNums1, minNums2) + Math.min(maxNums1, maxNums2)) / 2.0;
}
return Math.max(minNums1, minNums2);
} else if (minNums1 > maxNums2) {
end = partition1 - 1;
} else {
start = partition1 + 1;
}
}
return 0;
}
}