-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidoftwosortedarrays.cpp
More file actions
51 lines (51 loc) · 1.41 KB
/
midoftwosortedarrays.cpp
File metadata and controls
51 lines (51 loc) · 1.41 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
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m = nums1.size();
int n = nums2.size();
int total = m + n;
if (total & 0x1)
{
return findkth(nums1, m, nums2, n, total / 2 + 1);
}
else
{
return (findkth(nums1, m, nums2, n, total / 2 ) +
findkth(nums1, m, nums2, n, total / 2 + 1)) / 2;
}
}
private:
double findkth(vector<int>&nums1, int lena, vector<int>&nums2, int lenb, int k)
{
if (lena > lenb)
{
return findkth(nums2, lenb, nums1, lena, k);
}
if (lena == 0)
{
return nums2[k - 1];
}
if (k == 1)
{
return min(nums1[0], nums2[0]);
}
int pa = min(k / 2, lena);
int pb = k - pa;
if (nums1[pa - 1] < nums2[pb - 1])
{
vector<int>nums;
nums.assign(nums1.begin() + pa, nums1.end());
return findkth(nums, lena - pa, nums2, lenb, k - pa);
}
else if (nums1[pa-1] > nums2[pb-1])
{
vector<int>nums;
nums.assign(nums2.begin() + pb, nums2.end());
return findkth(nums1, lena, nums, lenb - pb, k - pb);
}
else
{
return nums1[pa-1];
}
}
};