-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedianOfTwoSortedArray.cc
More file actions
144 lines (133 loc) · 4.31 KB
/
MedianOfTwoSortedArray.cc
File metadata and controls
144 lines (133 loc) · 4.31 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
namespace MedianOfTwoSortedArray {
// this solution has complexity O(m + n)
class Solution1 {
public:
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int i = 0;
int j = 0;
int mid = (m + n) / 2;
bool odd = ((m + n) % 2 == 1);
int count = 0;
vector<int> medians;
while (i < m && j < n) {
int cur;
if (A[i] < B[j]) {
cur = A[i];
i++;
} else {
cur = B[j];
j++;
}
if (count == mid - 1) {
medians.push_back(cur);
} else if (count == mid) {
medians.push_back(cur);
break;
}
count++;
}
if (medians.size() == 2) {
if (odd) {
return medians[1];
} else {
return (medians[0] + medians[1]) / 2.0;
}
}
if (medians.size() == 1) {
if (odd) {
if (i < m) {
return A[i];
} else {
return B[j];
}
} else {
if (i < m) {
return (medians[0] + A[i]) / 2.0;
} else {
return (medians[0] + B[j]) / 2.0;
}
}
}
if (medians.size() == 0) {
if (odd) {
if (i < m) {
return A[i + mid - count];
} else {
return B[j + mid - count];
}
} else {
if (i < m) {
return (A[i + mid - 1 - count] + A[i + mid - count]) / 2.0;
} else {
return (B[j + mid - 1- count] + B[j + mid - count]) / 2.0;
}
}
}
}
// this solution is O(log(m + n))
class Solution {
public:
double findKthElement(int A[], int m, int B[], int n, int k) {
int startA = 0;
int startB = 0;
int ka, kb;
while (true) {
if (startA == m) return B[startB + k];
if (startB == n) return A[startA + k];
if (k == 0) return min(A[startA], B[startB]);
if (k == 1) {
ka = startA;
kb = startB;
} else {
ka = min(m - 1, startA + k / 2 - 1);
kb = min(n - 1, startB + k / 2 - 1);
}
if (A[ka] <= B[kb]) {
k -= (ka - startA + 1);
startA = ka + 1;
} else {
k -= (kb - startB + 1);
startB = kb + 1;
}
}
}
double findMedianSortedArrays(int A[], int m, int B[], int n) {
int mid = (m + n) / 2;
if ((m + n) % 2 == 1) {
return findKthElement(A, m, B, n, mid);
} else {
return (findKthElement(A, m, B, n, mid - 1) + findKthElement(A, m, B, n, mid)) / 2.0;
}
}
};
// another O(log(m + n)) solution
class Solution {
public:
// k starts with 1
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;
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;
}
};
}