-
Notifications
You must be signed in to change notification settings - Fork 0
/
191003-2.cpp
123 lines (110 loc) · 2.78 KB
/
191003-2.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
// https://leetcode-cn.com/problems/median-of-two-sorted-arrays/
#include <cstdio>
#include <vector>
using namespace std;
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n1 = nums1.size();
int n2 = nums2.size();
int p1 = 0, q1 = n1, k1 = n1 / 2;
int p2 = 0, q2 = n2, k2 = (n1 + n2) / 2 - k1;
for (;;) {
if (k1 > 0 && k2 < n2 && nums1[k1 - 1] > nums2[k2]) {
int s = (q2 - k2) / 2;
if (s > k1 - p1) s = k1 - p1;
if (s == 0) s = 1;
q1 = k1 - 1; p2 = k2 + 1;
k1 -= s; k2 += s;
continue;
} else if (k2 > 0 && k1 < n1 && nums2[k2 - 1] > nums1[k1]) {
int s = (q1 - k1) / 2;
if (s > k2 - p2) s = k2 - p2;
if (s == 0) s = 1;
p1 = k1 + 1; q2 = k2 - 1;
k1 += s; k2 -= s;
continue;
}
break;
}
double v;
if (k1 < n1 && k2 < n2) {
v = (nums1[k1] <= nums2[k2] ? nums1[k1] : nums2[k2]);
} else if (k1 < n1) {
v = nums1[k1];
} else if (k2 < n2) {
v = nums2[k2];
} else {
v = 0;
}
if ((n1 + n2) % 2 == 0) {
double v2;
if (k1 > 0 && k2 > 0) {
v2 = (nums1[k1 - 1] >= nums2[k2 - 1] ? nums1[k1 - 1] : nums2[k2 - 1]);
} else if (k1 > 0) {
v2 = nums1[k1 - 1];
} else if (k2 > 0) {
v2 = nums2[k2 - 1];
} else {
v2 = 0;
}
v = (v + v2) / 2;
}
return v;
}
};
int main()
{
Solution s;
{
vector<int> nums1 = { 1, 3 };
vector<int> nums2 = { 2 };
printf("%f\n", s.findMedianSortedArrays(nums1, nums2)); // answer: 2.0
}
{
vector<int> nums1 = { 1, 2 };
vector<int> nums2 = { 3, 4 };
printf("%f\n", s.findMedianSortedArrays(nums1, nums2)); // answer: 2.5
}
{
vector<int> nums1 = { 1, 2, 3 };
vector<int> nums2 = { 1, 2 };
printf("%f\n", s.findMedianSortedArrays(nums1, nums2)); // answer: 2
}
{
vector<int> nums1 = { 2, 3, 4 };
vector<int> nums2 = { 1 };
printf("%f\n", s.findMedianSortedArrays(nums1, nums2)); // answer: 2.5
}
{
vector<int> nums1 = { 2, 3, 4 };
vector<int> nums2 = { };
printf("%f\n", s.findMedianSortedArrays(nums1, nums2)); // answer: 3
}
{
vector<int> nums1 = { 1, 3, 5 };
vector<int> nums2 = { 2, 4, 6 };
printf("%f\n", s.findMedianSortedArrays(nums1, nums2)); // answer: 3.5
}
{
vector<int> nums1 = { 100000 };
vector<int> nums2 = { 100001 };
printf("%f\n", s.findMedianSortedArrays(nums1, nums2)); // answer: 100000.5
}
{
vector<int> nums1 = { 1, 2, 4 };
vector<int> nums2 = { 3, 5, 6, 7 };
printf("%f\n", s.findMedianSortedArrays(nums1, nums2)); // answer: 4
}
{
vector<int> nums1 = { 4, 5, 6, 7 };
vector<int> nums2 = { 1 };
printf("%f\n", s.findMedianSortedArrays(nums1, nums2)); // answer: 5
}
{
vector<int> nums1 = { 1, 5, 6, 7 };
vector<int> nums2 = { 1, 6 };
printf("%f\n", s.findMedianSortedArrays(nums1, nums2)); // answer: 5.5
}
return 0;
}