-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathmedian_of_two_sorted_arrays.js
78 lines (68 loc) · 2.3 KB
/
median_of_two_sorted_arrays.js
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
76
77
78
/*
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).
Example 1:
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.
Example 2:
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
Constraints:
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106
*/
// Approach 1: Brute force
function findMedianSortedArrays(arr1, arr2) {
const merged = arr1.concat(arr2).sort((a, b) => a - b);
const n = merged.length;
if (n % 2 === 0) {
const middle = n / 2;
return (merged[middle - 1] + merged[middle]) / 2;
} else {
const middle = Math.floor(n / 2);
return merged[middle];
}
}
// Approach 2: Binary Search
function findMedianSortedArrays(arr1, arr2) {
// If arr1 is longer than arr2, swap them to ensure arr1 is shorter
if (arr1.length > arr2.length) {
[arr1, arr2] = [arr2, arr1];
}
const m = arr1.length;
const n = arr2.length;
let left = 0;
let right = m;
while (left <= right) {
// Partition arr1 and arr2
const partition1 = Math.floor((left + right) / 2);
const partition2 = Math.floor((m + n + 1) / 2) - partition1;
// Calculate the max and min elements of the left and right partitions
const maxLeft1 = partition1 === 0 ? -Infinity : arr1[partition1 - 1];
const minRight1 = partition1 === m ? Infinity : arr1[partition1];
const maxLeft2 = partition2 === 0 ? -Infinity : arr2[partition2 - 1];
const minRight2 = partition2 === n ? Infinity : arr2[partition2];
// If the partitions are correctly balanced, return the median
if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
if ((m + n) % 2 === 0) {
return (
(Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2
);
} else {
return Math.max(maxLeft1, maxLeft2);
}
} else if (maxLeft1 > minRight2) {
// If maxLeft1 is too big, move partition1 to the left
right = partition1 - 1;
} else {
// If maxLeft2 is too big, move partition1 to the right
left = partition1 + 1;
}
}
}