-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedia_of_Two_Sorted_Arrays.cpp
More file actions
65 lines (59 loc) · 2 KB
/
Media_of_Two_Sorted_Arrays.cpp
File metadata and controls
65 lines (59 loc) · 2 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
56
57
58
59
60
61
62
63
64
65
/*
better than anson's, maybe I met this problem half a year ago, now, I just can't remember
findKth, awsome!always let a be the shorter array
two pointer running, the second way, cool too
*/
//O(log(min(M,N)))
class Solution
{
public:
double findKth(int a[], int m, int b[], int n, int 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 min(a[0], b[0]);
//divide k into two parts
int pa = min(k / 2, m), pb = k - pa;
//int pa=(m/(m+n)*1.0)*k, pb=k-pa;
if (a[pa - 1] < b[pb - 1])
return findKth(a + pa, m - pa, b, n, k - pa);
else if (a[pa - 1] > b[pb - 1])
return findKth(a, m, b + pb, n - pb, k - pb);
else
return a[pa - 1];
}
double findMedianSortedArrays(int A[], int m, int B[], int n)
{
int total = m + n;
if (total & 0x1)
return findKth(A, m, B, n, total / 2 + 1);
else
return (findKth(A, m, B, n, total / 2)
+ findKth(A, m, B, n, total / 2 + 1)) / 2;
}
double findMedianSortedArrays1(int A[], int m, int B[], int n) {
int i = 0, j = 0;
int m1 = -1, m2 = -1;
int s = (m + n) / 2;
while (s >= 0) {
int a = (i < m) ? A[i] : INT_MAX;
int b = (j < n) ? B[j] : INT_MAX;
m1 = m2;
if (a < b) {
m2 = a;
i++;
}
else {
m2 = b;
j++;
}
s--;
}
if ((m + n) % 2 == 0) return (m1 + m2) / 2.0;
return m2;
};
};