-
Notifications
You must be signed in to change notification settings - Fork 0
/
median_sorted_arrays.cpp
188 lines (170 loc) · 4.71 KB
/
median_sorted_arrays.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// =====================================================================================
//
// Filename: median_sorted_arrays.cpp
//
// Description: 4. Median of Two Sorted Arrays.
//
// Version: 1.0
// Created: 11/11/2019 08:14:01 PM
// Revision: none
// Compiler: g++
//
// Author: Zhu Xianfeng (), [email protected]
// Organization:
//
// =====================================================================================
#include <tuple>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::tuple;
using std::vector;
// Binary search
class Solution1 {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if (nums1.size() > nums2.size()) {
return findMedianSortedArrays(nums2, nums1);
}
if (nums2.size() == 0) {
return 0.0;
}
// Ensure m <= n
const int m = nums1.size();
const int n = nums2.size();
int left = 0;
int right = m;
int i = 0;
int j = 0;
// left_part | right_part
// A[0], A[1], ..., A[i-1] | A[i], A[i+1], ..., A[m-1]
// B[0], B[1], ..., B[j-1] | B[j], B[j+1], ..., B[n-1]
while (left <= right) {
i = (left + right) / 2;
j = (m + n + 1) / 2 - i;
if (i < m && nums1[i] < nums2[j - 1]) {
// i is too small
left = i + 1;
} else if (i > 0 && nums1[i - 1] > nums2[j]) {
// i is too big
right = i - 1;
} else {
// Found
// i == 0 || i == m || nums1[i] >= nums2[j - 1]
// j == 0 || j == n || nums1[i - 1] <= nums2[j]
break;
}
}
int left_max = 0;
int right_min = 0;
if (i == 0) {
left_max = nums2[j - 1];
} else if (j == 0) {
left_max = nums1[i - 1];
} else {
left_max = std::max(nums1[i - 1], nums2[j - 1]);
}
if ((m + n) & 0x1) {
// Avoid j overflow
return (double)left_max;
}
if (i == m) {
right_min = nums2[j];
} else if (j == n) {
right_min = nums1[i];
} else {
right_min = std::min(nums1[i], nums2[j]);
}
return ((left_max + right_min) / 2.0);
}
};
// Intuitive: O(m+n)
class Solution2 {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
const int m = (nums1.size() + nums2.size() - 1) / 2;
const int n = (nums1.size() + nums2.size()) / 2;
int steps = 0;
double median = 0.0;
auto apply = [&](int val) {
if (steps == m) {
median = val;
}
if (steps == n) {
median = (median + val) / 2.0;
return true;
}
return false;
};
bool ok = false;
for (int i = 0, j = 0;; steps++) {
if (i < nums1.size() && j < nums2.size()) {
if (nums1[i] < nums2[j]) {
ok = apply(nums1[i]);
i++;
} else {
ok = apply(nums2[j]);
j++;
}
} else if (i < nums1.size()) {
ok = apply(nums1[i]);
i++;
} else {
ok = apply(nums2[j]);
j++;
}
if (ok) {
break;
}
}
return median;
}
};
// Binary search
class Solution3 {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
const int m = nums1.size();
const int n = nums2.size();
if (m > n) {
// Ensure m <= n
return findMedianSortedArrays(nums2, nums1);
}
const int target = (m + n + 1) / 2;
int left = 0;
int right = m;
while (left <= right) {
const int i = (left + right) / 2;
const int j = target - i;
const int min1 = (i > 0 ? nums1[i - 1] : INT_MIN);
const int max1 = (i < m ? nums1[i] : INT_MAX);
const int min2 = (j > 0 ? nums2[j - 1] : INT_MIN);
const int max2 = (j < n ? nums2[j] : INT_MAX);
if (min1 > max2) {
right = i - 1;
} else if (max1 < min2) {
left = i + 1;
} else {
// min1 <= max2 && min2 <= max1
if ((m + n) & 0x1) {
return std::max(min1, min2);
} else {
return (std::max(min1, min2) + std::min(max1, max2)) / 2.0;
}
}
}
return 0.0;
}
};
TEST(Solution, findMedianSortedArrays) {
vector<tuple<vector<int>, vector<int>, double>> cases = {
std::make_tuple(vector<int>{}, vector<int>{2}, 2.0),
std::make_tuple(vector<int>{1, 3}, vector<int>{2}, 2.0),
std::make_tuple(vector<int>{1, 2}, vector<int>{3, 4}, 2.5),
};
for (auto& c : cases) {
EXPECT_EQ(Solution1().findMedianSortedArrays(std::get<0>(c), std::get<1>(c)), std::get<2>(c));
EXPECT_EQ(Solution2().findMedianSortedArrays(std::get<0>(c), std::get<1>(c)), std::get<2>(c));
EXPECT_EQ(Solution3().findMedianSortedArrays(std::get<0>(c), std::get<1>(c)), std::get<2>(c));
}
}