Median of Two Sorted Arrays
2015-07-31 10:51
204 查看
There are two sorted arrays nums1 and nums2 of
size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
中位数是第K个数,K = (m + n)/2,,不考虑奇偶性可以这样理解,所以问题就转换为了如何求出两个排序数组的第K小数;如果不考虑O(log(m + n))的时间复杂度,可以利用一个计数器在合并两个数组的同时技术,找到K个数就结束合并,所以时间复杂度是O(K),即O((m
+ n)/2);
为了更加高效的解决这个问题,结合求第K小数的经验,我们应该考虑类似于二分的操作。怎么二分是个问题,这里选择将K二分,即首先找到前K/2个元素,因为数组A,B都是有序的,一次我们只要比较A[K/2 - 1]与B[K/2 -1]的大小,如果等于,则说明就是这个数,直接返回即可;如果A[K/2
- 1]小于B[K/2 -1], 则一定有前K/2个数就是A中前K/2 个数,反之则是B中的前K/2个数;然后递归查找,知道K == 1,或者 最小的数组都搜索完。
PS:对这一题进行延伸,可以用来查找两个已排序数组的任意第K小数
double findKth(vector<int> &nums1, int m, int curStart1, vector<int> &nums2, int n, int curStart2, int k)
{
int tmpm = m - curStart1;
int tmpn = n - curStart2;
if(tmpm > tmpn)
return findKth(nums2, n, curStart2, nums1, m, curStart1, k);
if(tmpm == 0)
return nums2[k - 1];
if(k == 1)
return min(nums1[curStart1], nums2[curStart2]);
int pa = min(k/2, tmpm), pb = k - pa;
if(nums1[curStart1 + pa - 1] < nums2[curStart2 + pb - 1])
return findKth(nums1, m, curStart1 + pa, nums2, n, curStart2, k - pa);
else if(nums1[curStart1 + pa - 1] > nums2[curStart2 + pb - 1])
return findKth(nums1, m, curStart1, nums2, n, curStart2 + pb, k - pb);
else
return nums1[curStart1 + pa - 1];
}
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2) {
int m = nums1.size();
int n = nums2.size();
int total = m + n;
if (total == 0)
return 0;
if (total == m || total == n)
{
if (total == m)
{
if (total % 2 == 0)
return (nums1[total / 2] + nums1[total / 2 - 1]) / 2.0;
else
return nums1[total / 2];
}
else
{
if (total % 2 == 0)
return (nums2[total / 2] + nums2[total / 2 - 1]) / 2.0;
else
return nums2[total / 2];
}
}
if (total & 0x1)
return findKth(nums1, m, 0, nums2, n, 0, total / 2 + 1);
else
return (findKth(nums1, m, 0, nums2, n, 0, total / 2)
+ findKth(nums1, m, 0, nums2, n, 0, total / 2 + 1)) / 2;
}
size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
中位数是第K个数,K = (m + n)/2,,不考虑奇偶性可以这样理解,所以问题就转换为了如何求出两个排序数组的第K小数;如果不考虑O(log(m + n))的时间复杂度,可以利用一个计数器在合并两个数组的同时技术,找到K个数就结束合并,所以时间复杂度是O(K),即O((m
+ n)/2);
为了更加高效的解决这个问题,结合求第K小数的经验,我们应该考虑类似于二分的操作。怎么二分是个问题,这里选择将K二分,即首先找到前K/2个元素,因为数组A,B都是有序的,一次我们只要比较A[K/2 - 1]与B[K/2 -1]的大小,如果等于,则说明就是这个数,直接返回即可;如果A[K/2
- 1]小于B[K/2 -1], 则一定有前K/2个数就是A中前K/2 个数,反之则是B中的前K/2个数;然后递归查找,知道K == 1,或者 最小的数组都搜索完。
PS:对这一题进行延伸,可以用来查找两个已排序数组的任意第K小数
double findKth(vector<int> &nums1, int m, int curStart1, vector<int> &nums2, int n, int curStart2, int k)
{
int tmpm = m - curStart1;
int tmpn = n - curStart2;
if(tmpm > tmpn)
return findKth(nums2, n, curStart2, nums1, m, curStart1, k);
if(tmpm == 0)
return nums2[k - 1];
if(k == 1)
return min(nums1[curStart1], nums2[curStart2]);
int pa = min(k/2, tmpm), pb = k - pa;
if(nums1[curStart1 + pa - 1] < nums2[curStart2 + pb - 1])
return findKth(nums1, m, curStart1 + pa, nums2, n, curStart2, k - pa);
else if(nums1[curStart1 + pa - 1] > nums2[curStart2 + pb - 1])
return findKth(nums1, m, curStart1, nums2, n, curStart2 + pb, k - pb);
else
return nums1[curStart1 + pa - 1];
}
double findMedianSortedArrays(vector<int> &nums1, vector<int> &nums2) {
int m = nums1.size();
int n = nums2.size();
int total = m + n;
if (total == 0)
return 0;
if (total == m || total == n)
{
if (total == m)
{
if (total % 2 == 0)
return (nums1[total / 2] + nums1[total / 2 - 1]) / 2.0;
else
return nums1[total / 2];
}
else
{
if (total % 2 == 0)
return (nums2[total / 2] + nums2[total / 2 - 1]) / 2.0;
else
return nums2[total / 2];
}
}
if (total & 0x1)
return findKth(nums1, m, 0, nums2, n, 0, total / 2 + 1);
else
return (findKth(nums1, m, 0, nums2, n, 0, total / 2)
+ findKth(nums1, m, 0, nums2, n, 0, total / 2 + 1)) / 2;
}
相关文章推荐
- 责任链模式(Chain of Responsibility)的标准版
- Validation of viewstate MAC failed.的解决方法
- Some tips of wmi scripting in jscript (1)
- Java设计模式之责任链模式(Chain of Responsibility模式)介绍
- python sort、sorted高级排序技巧
- python中List的sort方法指南
- python使用sorted函数对列表进行排序的方法
- identifier of an instance of com.edu.zidonghua.po.TbUser was altered from 2 to null
- The art of software testing翻译--第一章
- Check out the top five articles of 2006
- Security functions of common Active Directory tools
- Overview of Siebel Application Integration
- Get the SID of an Active Directory User
- java反射getConstructor()[0].newInstance(params)报wrong number of args的错误
- 数学符号,不常用的时候参考一下…
- LeetCode 2: Add Two Numbers (JAVA)
- LeetCode 3: Longest Substring Without Repea (JAVA)
- 解决EndnoteX5无法连接web of science的方案
- hdu 1316 How Many Fibs?
- 【LeetCode】23.Merge k Sorted Lists