class Solution {
int myMin(int x, int y) {
return x < y ? x : y;
}
int findNumber(vector<int>& nums1, vector<int>& nums2, int findKth) {
int length_1 = (int) nums1.size();
int length_2 = (int) nums2.size();
if (length_1 == 0) return nums2[ findKth - 1 ];
if (length_2 == 0) return nums1[ findKth - 1 ];
int left_1 = 0, left_2 = 0;
int mid_1 = 0, mid_2 = 0;
int k;
do {
if (length_1 - left_1 == 0) return nums2[ findKth - 1 ];
if (length_2 - left_2 == 0) return nums1[ findKth - 1 ];
if (findKth == 1) return myMin(nums1[left_1], nums2[left_2]);
k = findKth / 2;
if (length_2 - left_2 >= length_1 - left_1) {
if (length_1 - left_1 < k) k = length_1 - left_1;
mid_1 = left_1 + (k - 1);
mid_2 = left_2 + (findKth - k - 1);
} else {
if (length_2 - left_2 < k) k = length_2 - left_2;
mid_2 = left_2 + (k - 1);
mid_1 = left_1 + (findKth - k - 1);
}
if (nums1[mid_1] < nums2[mid_2]) {
findKth -= mid_1 - left_1 + 1;
left_1 = mid_1 + 1;
} else {
findKth -= mid_2 - left_2 + 1;
left_2 = mid_2 + 1;
}
} while(true);
}
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int totNumber = (int) nums1.size() + (int) nums2.size();
int findKth = totNumber / 2;
if (totNumber % 2 == 1) return findNumber(nums1, nums2, findKth + 1);
else return (findNumber(nums1, nums2, findKth) + findNumber(nums1, nums2, findKth + 1)) / 2.0;
}
};