-
Notifications
You must be signed in to change notification settings - Fork 154
/
median-of-two-sorted-arrays.js
72 lines (63 loc) · 1.5 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
/**
* Median of Two Sorted Arrays
*
* There are two sorted arrays nums1 and nums2 of size m and n respectively.
*
* Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
*
* Example 1:
* nums1 = [1, 3]
* nums2 = [2]
*
* The median is 2.0
* Example 2:
* nums1 = [1, 2]
* nums2 = [3, 4]
*
* The median is (2 + 3)/2 = 2.5
*/
/**
* Find the median from two sorted arrays
*
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
const findMedianSortedArrays = (nums1, nums2) => {
const k = Math.floor((nums1.length + nums2.length) / 2);
if ((nums1.length + nums2.length) % 2 === 0) {
return (findKth(nums1, nums2, k) + findKth(nums1, nums2, k + 1)) / 2;
} else {
return findKth(nums1, nums2, k + 1);
}
};
/**
* Find the k-th number from two sorted arrays
*
* @param {number[]} nums1
* @param {number[]} nums2
* @param {number} k
*/
const findKth = (nums1, nums2, k) => {
const m = nums1.length;
const n = nums2.length;
if (m > n) {
return findKth(nums2, nums1, k);
}
if (m === 0) {
return nums2[k - 1];
}
if (k === 1) {
return Math.min(nums1[0], nums2[0]);
}
const c1 = Math.min(Math.floor(k / 2), m);
const c2 = k - c1;
if (nums1[c1 - 1] === nums2[c2 - 1]) {
return nums1[c1 - 1];
} else if (nums1[c1 - 1] < nums2[c2 - 1]) {
return findKth(nums1.slice(c1), nums2, k - c1);
} else {
return findKth(nums1, nums2.slice(c2), k - c2);
}
};
export default findMedianSortedArrays;