-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.median-of-two-sorted-arrays.js
122 lines (112 loc) · 2.69 KB
/
4.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* @lc app=leetcode id=4 lang=javascript
*
* [4] Median of Two Sorted Arrays
*
* https://leetcode.com/problems/median-of-two-sorted-arrays/description/
*
* algorithms
* Hard (25.98%)
* Total Accepted: 412.6K
* Total Submissions: 1.6M
* Testcase Example: '[1,3]\n[2]'
*
* 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)).
*
* You may assume nums1 and nums2 cannot be both empty.
*
* 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
*
*
*/
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
/*
n log (n)
Unknown:Median of two sorted array
Divide and conquer:
1. Combine two sorted array to one - yes
2. Median of one sorted array(is Odd or event)
*/
function combineTwoSortedArray(nums1, nums2) {
//should have more efficient method
var s = nums1.concat(nums2);
s.sort(function(a, b) {
return a - b;
});
return s;
}
function isOdd(num) {
return num & 1;
}
function integerDivision(num) {
return ~~(num / 2);
}
function medianOfSortedArray(s) {
var len = s.length;
if (isOdd(len)) {
//the 6th number with index 5 for total 11 elements
return s[integerDivision(len)];
} else {
let secondMedianIndex = integerDivision(len);
return (s[secondMedianIndex - 1] + s[secondMedianIndex]) / 2;
}
}
var findMedianSortedArrays = function(nums1, nums2) {
let s = combineTwoSortedArray(nums1, nums2);
return medianOfSortedArray(s);
};
/*
log(n)
Unknown:Median of two sorted array
Divide and conquer:
1. Combine two sorted array to one - yes
2. Median of sorted array(is Odd or event)
*/
// var findMedianSortedArrays = function(nums1, nums2) {
// var m = nums1.length;
// var n = nums2.length;
// var total = m + n;
// var half = integerDivision(total);
// if (isOdd(total)) return findKth(nums1, m, nums2, n, half + 1);
// else
// return (
// (findKth(nums1, m, nums2, n, half) +
// findKth(nums1, m, nums2, n, half + 1)) /
// 2
// );
// };
// function findKth(a, m, b, n, k) {
// // always assume that m is equal or smaller than n
// if (m > n) return findKth(b, n, a, m, k);
// if (m === 0) return b[k - 1];
// if (k === 1) return Math.min(a[0], b[0]);
// // divide k into two parts
// var pa = Math.min(k >> 1, m),
// pb = k - pa;
// if (a[pa - 1] < b[pb - 1]) return findKth(a.slice(pa), m - pa, b, n, k - pa);
// else if (a[pa - 1] > b[pb - 1])
// return findKth(a, m, b.slice(pb), n - pb, k - pb);
// else return a[pa - 1];
// }